@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
"runtime"
5
5
"sync"
6
+ "sync/atomic"
6
7
"time"
7
8
)
8
9
@@ -70,11 +71,13 @@ func main() {
70
71
// Test multi-receiver.
71
72
ch = make (chan int )
72
73
wg .Add (3 )
73
- go fastreceiver (ch )
74
- go fastreceiver (ch )
75
- go fastreceiver (ch )
74
+ var result atomic.Uint32
75
+ go fastreceiveradd (ch , & result )
76
+ go fastreceiveradd (ch , & result )
77
+ go fastreceiveradd (ch , & result )
76
78
slowsender (ch )
77
79
wg .Wait ()
80
+ println ("sum of sums:" , result .Load ())
78
81
79
82
// Test iterator style channel.
80
83
ch = make (chan int )
@@ -88,7 +91,10 @@ func main() {
88
91
println ("sum(100):" , sum )
89
92
90
93
// Test simple selects.
91
- go selectDeadlock () // cannot use waitGroup here - never terminates
94
+ wg .Add (1 )
95
+ go selectDeadlock ()
96
+ wg .Wait ()
97
+
92
98
wg .Add (1 )
93
99
go selectNoOp ()
94
100
wg .Wait ()
@@ -244,7 +250,7 @@ func receive(ch <-chan int) {
244
250
func sender (ch chan int ) {
245
251
for i := 1 ; i <= 8 ; i ++ {
246
252
if i == 4 {
247
- time .Sleep (time .Microsecond )
253
+ time .Sleep (time .Millisecond )
248
254
println ("slept" )
249
255
}
250
256
ch <- i
@@ -290,6 +296,16 @@ func fastreceiver(ch chan int) {
290
296
wg .Done ()
291
297
}
292
298
299
+ func fastreceiveradd (ch chan int , result * atomic.Uint32 ) {
300
+ sum := 0
301
+ for i := 0 ; i < 2 ; i ++ {
302
+ n := <- ch
303
+ sum += n
304
+ }
305
+ result .Add (uint32 (sum ))
306
+ wg .Done ()
307
+ }
308
+
293
309
func iterator (ch chan int , top int ) {
294
310
for i := 0 ; i < top ; i ++ {
295
311
ch <- i
@@ -300,6 +316,7 @@ func iterator(ch chan int, top int) {
300
316
301
317
func selectDeadlock () {
302
318
println ("deadlocking" )
319
+ wg .Done ()
303
320
select {}
304
321
println ("unreachable" )
305
322
}
0 commit comments