Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CSP
package main
import (
"log"
"time"
)
type cond struct {
ch1 chan struct{}
ch2 chan struct{}
ch3 chan struct{}
sema1 int32
sema2 int32
}
func (c *cond) printA() {
for {
<-c.ch1
log.Println("A")
c.ch2 <- struct{}{}
}
}
func (c *cond) printB() {
for {
<-c.ch2
log.Println("B")
c.ch3 <- struct{}{}
}
}
func (c *cond) printC() {
for {
<-c.ch3
log.Println("C")
log.Println("--------------------------------")
c.ch1 <- struct{}{}
}
}
func main() {
var con = new(cond)
con.ch1 = make(chan struct{})
con.ch2 = make(chan struct{})
con.ch3 = make(chan struct{})
go con.printC()
go con.printB()
go con.printA()
con.ch1 <- struct{}{}
time.Sleep(1 * time.Second)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment