Skip to content

Commit e1d0fea

Browse files
committed
use of go channels and deadlock
1 parent 54588d9 commit e1d0fea

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

go2Channels.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ import (
55
"time"
66
)
77

8-
func attack2(target string) {
8+
func attack2(target string, ensure chan bool) {
99
time.Sleep(1 * time.Second)
1010
fmt.Println(target)
11+
ensure <- true
1112
}
1213

1314
func channelsImplementation() {
1415
// Create a channel
1516
ninja := "Vibhor"
17+
channel := make(chan bool)
1618

17-
go attack2(ninja)
18-
19-
time.Sleep(2 * time.Second)
19+
go attack2(ninja, channel)
20+
channel <- false // Will lead to dealock
21+
fmt.Println("Waiting for the goroutine to finish")
22+
for !<-channel {
23+
}
24+
// Wait for the goroutine to finish
2025
}

0 commit comments

Comments
 (0)