zoukankan      html  css  js  c++  java
  • 将通道用做互斥锁(mutex)

    通道用例大全 - Go语言101(通俗版Go白皮书) https://gfw.go101.org/article/channel-use-cases.html

    将通道用做互斥锁(mutex)

    上面的某个例子提到了容量为1的缓冲通道可以用做一次性二元信号量。 事实上,容量为1的缓冲通道也可以用做多次性二元信号量(即互斥锁)尽管这样的互斥锁效率不如sync标准库包中提供的互斥锁高效。

    有两种方式将一个容量为1的缓冲通道用做互斥锁:
    1. 通过发送操作来加锁,通过接收操作来解锁;
    2. 通过接收操作来加锁,通过发送操作来解锁。

     

    下面是一个通过发送操作来加锁的例子。
    package main
    
    import "fmt"
    
    func main() {
    	mutex := make(chan struct{}, 1) // 容量必须为1
    
    	counter := 0
    	increase := func() {
    		mutex <- struct{}{} // 加锁
    		counter++
    		<-mutex // 解锁
    	}
    
    	increase1000 := func(done chan<- struct{}) {
    		for i := 0; i < 1000; i++ {
    			increase()
    		}
    		done <- struct{}{}
    	}
    
    	done := make(chan struct{})
    	go increase1000(done)
    	go increase1000(done)
    	<-done; <-done
    	fmt.Println(counter) // 2000
    }
    

     

    下面是一个通过接收操作来加锁的例子,其中只显示了相对于上例而修改了的部分。
    ...
    func main() {
    	mutex := make(chan struct{}, 1)
    	mutex <- struct{}{} // 此行是必需的
    
    	counter := 0
    	increase := func() {
    		<-mutex // 加锁
    		counter++
    		mutex <- struct{}{} // 解锁
    	}
    ...
    

     

    将通道用做互斥锁(mutex)

    上面的某个例子提到了容量为1的缓冲通道可以用做一次性二元信号量。 事实上,容量为1的缓冲通道也可以用做多次性二元信号量(即互斥锁)尽管这样的互斥锁效率不如sync标准库包中提供的互斥锁高效。

    有两种方式将一个容量为1的缓冲通道用做互斥锁:
    1. 通过发送操作来加锁,通过接收操作来解锁;
    2. 通过接收操作来加锁,通过发送操作来解锁。

     

    下面是一个通过发送操作来加锁的例子。
    package main
    
    import "fmt"
    
    func main() {
    	mutex := make(chan struct{}, 1) // 容量必须为1
    
    	counter := 0
    	increase := func() {
    		mutex <- struct{}{} // 加锁
    		counter++
    		<-mutex // 解锁
    	}
    
    	increase1000 := func(done chan<- struct{}) {
    		for i := 0; i < 1000; i++ {
    			increase()
    		}
    		done <- struct{}{}
    	}
    
    	done := make(chan struct{})
    	go increase1000(done)
    	go increase1000(done)
    	<-done; <-done
    	fmt.Println(counter) // 2000
    }
    

     

    下面是一个通过接收操作来加锁的例子,其中只显示了相对于上例而修改了的部分。
    ...
    func main() {
    	mutex := make(chan struct{}, 1)
    	mutex <- struct{}{} // 此行是必需的
    
    	counter := 0
    	increase := func() {
    		<-mutex // 加锁
    		counter++
    		mutex <- struct{}{} // 解锁
    	}
    ...
    

     

     

  • 相关阅读:
    hibernate更新
    让hibernate支持mysql的text或者longtext
    hibernate的查询
    微博获取原图时重定向到图片的url
    java判断字符串编码
    爬取微博存入数据库时出错
    循环监听事件出错,只能监听最后一个
    org.hibernate.AssertionFailure: null id don't flus
    eclipse有时候导入了包,但是还是有红线,找不到
    hibernate注解创建表总是失败,显示表不存在
  • 原文地址:https://www.cnblogs.com/rsapaper/p/15691190.html
Copyright © 2011-2022 走看看