zoukankan      html  css  js  c++  java
  • golang sync.RWMutex

    sync.RWMutex
    package main
    
    import (
        "fmt"
        "runtime"
        "sync"
    )
    
    func clickWithMutex(total *int, m *sync.RWMutex, ch chan int) {
        for i := 0; i < 1000; i++ {
            m.Lock()
            *total += 1
            m.Unlock()
            //这里是写 下面是读,外层还有线程的竞争
            if i == 500 {
                m.RLock()
                fmt.Println(*total)
                m.RUnlock()
            }
        }
        ch <- 1
    }
    
    func main() {
    
        runtime.GOMAXPROCS(4) //使用多个处理器,不然都是顺序执行。
    
        m := new(sync.RWMutex)
        count := 0
    
        ch := make(chan int, 10) //保证输出时count完了
    
        for i := 0; i < 10; i++ {
            go clickWithMutex(&count, m, ch)
        }
    
        for i := 0; i < 10; i++ {
            <-ch
        }
    
        fmt.Printf("count:%d
    ", count)
    }
  • 相关阅读:
    openresty
    ATS 相关
    pandas
    flask
    ansible
    zipline
    bcolz
    数据分析 --- concat
    Go --- 基础使用
    Go --- 基础介绍
  • 原文地址:https://www.cnblogs.com/rojas/p/4408707.html
Copyright © 2011-2022 走看看