golang实现协程安全的几种方式

  • 版本
    golang -- 1.12.4

  • golang协程同步

1.channel - monitor goroutine

var deposits = make(chan int) // send amount to deposit
var balances = make(chan int) // receive balance

func Deposit(amount int) { deposits <- amount }
func Balance() int       { return <-balances }

func teller() {
     var balance int // balance is confined to teller goroutine
     for {
         select {
         case amount := <-deposits:
              balance += amount
         case balances <- balance:
         }
      }
}
func init() {
     go teller() // start the monitor goroutine
}

2.channel - serial confinement

type Cake struct{ state string }

func baker(cooked chan<- *Cake) {
     for {
             cake := new(Cake)
             cake.state = "cooked"
             cooked <- cake // baker never touches this cake again
         } 
}

func icer(iced chan<- *Cake, cooked <-chan *Cake) {
      for cake := range cooked {
             cake.state = "iced"
             iced <- cake // icer never touches this cake again
      } 
}

3.mutual exclusion

import "sync"

var mu      sync.Mutex // guards balance
var balance int

func Deposit(amount int) {
         mu.Lock()
         balance = balance + amount
         mu.Unlock()
}

func Balance() int {
         mu.Lock()
         defer mu.Unlock()
         return balance
}

4.mutual exclusion - RWMutex

import "sync"

var mu      sync.RWMutex // guards balance
var balance int

func Deposit(amount int) {
         mu.Lock()
         balance = balance + amount
         mu.Unlock()
}

func Balance() int {
         mu.RLock() //readers lock
         defer mu.RUnlock()
         return balance
}

RLock允许读取并行,写入和读取完全互斥,多次读取,一次写入

5.Lazy Initialization - sync.Once

var loadIconsOnce sync.Once
var icons map[string]image.Image
// Concurrency-safe.
func Icon(name string) image.Image {
     loadIconsOnce.Do(loadIcons)
     return icons[name]
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 介绍 如何保证在一个goroutine中看到在另一个goroutine修改的变量的值,这篇文章进行了详细说明。 建...
    51reboot阅读 20,008评论 11 41
  • Go(又称Golang)是Google开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的编程语言。Go于2...
    臣以君纲阅读 3,885评论 0 4
  • 开发go程序的时候,时常需要使用goroutine并发处理任务,有时候这些goroutine是相互独立的,而有的时...
    驻马听雪阅读 2,587评论 0 21
  • Go的内存模型 看完这篇文章你会明白 一个Go程序在启动时的执行顺序 并发的执行顺序 并发环境下如何保证数据的同步...
    初级赛亚人阅读 2,978评论 0 2
  • 你有梦 我有梦 我们遇见了 你背弃 我受了 却不曾想 你却恳求回头 我收了 却不料想 你变了 那样肆无忌惮 那样痛...
    海市蜃楼你的眼阅读 232评论 0 1

友情链接更多精彩内容