File tree Expand file tree Collapse file tree 4 files changed +59
-19
lines changed
Expand file tree Collapse file tree 4 files changed +59
-19
lines changed Original file line number Diff line number Diff line change @@ -69,3 +69,17 @@ func (me *CountdownLatch) CurrentCount() int {
6969
7070 return int (me .count .Load ())
7171}
72+
73+ // Reset は、カウントを指定された値にリセットします.
74+ func (me * CountdownLatch ) Reset (count int ) {
75+ if count < 0 {
76+ panic ("リセットカウントは0以上である必要があります" )
77+ }
78+
79+ me .mutex .Lock ()
80+ defer me .mutex .Unlock ()
81+
82+ me .cond .Broadcast ()
83+
84+ me .count .Store (int32 (count ))
85+ }
Original file line number Diff line number Diff line change @@ -63,32 +63,40 @@ func run(pCtx context.Context) context.Context {
6363
6464func proc (_ context.Context ) error {
6565 const (
66- latchCount = 3
66+ numLatchs = 3
67+ numGoroutines = 5
6768 )
6869 var (
69- latch = NewCountdownLatch (latchCount )
70- wg sync.WaitGroup
70+ latch = NewCountdownLatch (numLatchs )
7171 )
72+ for range 2 {
73+ var (
74+ wg sync.WaitGroup
75+ )
76+
77+ latch .Reset (numLatchs )
78+
79+ for i := range numGoroutines {
80+ wg .Add (1 )
81+ go func (i int ) {
82+ defer wg .Done ()
83+
84+ log .Printf ("[%2d] 待機開始" , i )
85+ latch .Wait ()
86+ log .Printf ("[%2d] 待機解除" , i )
87+ }(i )
88+ }
7289
73- for i := range 5 {
74- wg .Add (1 )
75- go func (i int ) {
76- defer wg .Done ()
77-
78- log .Printf ("[%2d] 待機開始" , i )
79- latch .Wait ()
80- log .Printf ("[%2d] 待機解除" , i )
81- }(i )
82- }
90+ for range numLatchs {
91+ <- time .After (time .Second )
8392
84- for range 3 {
85- <- time .After (time .Second )
93+ log .Printf ("現在のカウント: %d\n " , latch .CurrentCount ())
94+ latch .Signal ()
95+ }
8696
87- log . Printf ( "現在のカウント: %d \n " , latch . CurrentCount () )
88- latch . Signal ( )
97+ wg . Wait ( )
98+ log . Println ( "----------------" )
8999 }
90100
91- wg .Wait ()
92-
93101 return nil
94102}
Original file line number Diff line number Diff line change @@ -69,3 +69,17 @@ func (me *CountdownLatch) CurrentCount() int {
6969
7070 return int (me .count .Load ())
7171}
72+
73+ // Reset は、カウントを指定された値にリセットします.
74+ func (me * CountdownLatch ) Reset (count int ) {
75+ if count < 0 {
76+ panic ("リセットカウントは0以上である必要があります" )
77+ }
78+
79+ me .mutex .Lock ()
80+ defer me .mutex .Unlock ()
81+
82+ me .cond .Broadcast ()
83+
84+ me .count .Store (int32 (count ))
85+ }
Original file line number Diff line number Diff line change @@ -30,3 +30,7 @@ func (me *Gate) Open() {
3030
3131 me .latch .Signal ()
3232}
33+
34+ func (me * Gate ) Reset () {
35+ me .latch .Reset (1 )
36+ }
You can’t perform that action at this time.
0 commit comments