Skip to content

Instantly share code, notes, and snippets.

@bwangelme
Created March 25, 2019 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwangelme/b44b427621b28f1a17b0cd7a3e6e94ae to your computer and use it in GitHub Desktop.
Save bwangelme/b44b427621b28f1a17b0cd7a3e6e94ae to your computer and use it in GitHub Desktop.
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