go

进程同步问题

Posted by 云起 on 2022-10-08
Estimated Reading Time 1 Minutes
Words 113 In Total
Viewed Times

进程同步问题

生产者-消费者问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//单向通道
func producer(out chan <- int){
for i:=0;i<10;i++{
out<-i*i
}
close(out)
}
func consumer(in <-chan int){
for n:=range in{
fmt.Println(n)
}
}
func main(){
ch := make(chan int)
go producer(ch)
consumer(ch)
}

读者-写者问题

允许多个进程同时对数据进行读操作,但是不允许读和写以及写和写操作同时发生。


If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !