Created
March 25, 2019 02:08
-
-
Save bwangelme/b44b427621b28f1a17b0cd7a3e6e94ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
var i int | |
var mu sync.Mutex | |
var endSignal = make(chan struct{}) | |
func threadPrint(threadNum int, threadName string) { | |
for { | |
mu.Lock() | |
if i >= 30 { | |
mu.Unlock() | |
break | |
} | |
if i%3 == threadNum { | |
fmt.Println(threadName) | |
i++ | |
} | |
mu.Unlock() | |
} | |
endSignal <- struct{}{} | |
} | |
func main() { | |
names := []string{"A", "B", "C"} | |
for idx, name := range names { | |
go threadPrint(idx, name) | |
} | |
for _ = range names { | |
<-endSignal | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment