12 | gorutine

2020-03-21  本文已影响0人  刀斧手何在
func TestGoroutine(t *testing.T){
    for i:=0 ; i< 5; i++ {
        go (func(i int){
            t.Log(i)
        })(i)
    }
    t.Log("hello")
}

使用 go 关键字创建 goroutine 时,被调用函数的返回值会被忽略

func TestGoroutine(t *testing.T){
    var wg sync.WaitGroup

    for i:=0 ; i< 5; i++ {
        wg.Add(1)
        go (func(i int){
            defer wg.Done()
            t.Log(i)
        })(i)
    }
    wg.Wait()
    t.Log("hello")
}
func TestGoroutine(t *testing.T){
    
    for i:=0 ; i< 5; i++ {
        go (func(){
            t.Log(i)
        })()
    }
    
    t.Log("hello")
}

go run -race xxx.go 使用race参数可以检测竞态条件

func TestGoroutine(t *testing.T){

    var lock sync.Mutex
    for i:=0 ; i< 5; i++ {
        lock.Lock()
        go (func(){
            defer lock.Unlock()
            t.Log(i)
        })()
    }

    t.Log("hello")
}

在读多写少的环境中,可以优先使用读写互斥锁(sync.RWMutex),它比互斥锁更加高效。sync 包中的 RWMutex 提供了读写互斥锁的封装

runtime.GOMAXPROCS(runtime.NumCPU())

thead vs groutine

groutime vs coroutine

上一篇 下一篇

猜你喜欢

热点阅读