go 协程返回值处理

golang 协程返回值处理

package main

import "fmt"
import "sync"

var ch = make(chan int)

func do(lock *sync.Mutex, ct *int) {
    lock.Lock()
    *ct++
    lock.Unlock()
    ch <- 1
}


func main() {
    fmt.Println("hello thread")
    var ct = 0
    lock := &sync.Mutex{}
    for i:=0; i<10; i++ {
        go do(lock, &ct)
    }

    for i:=0; i<10; i++ {
        <- ch
    }
    fmt.Println("ct=", ct)
}

输出: 10

你可能感兴趣的:(golang)