• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Gosync模块

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

// A WaitGroup waits for a collection of goroutines to finish.
// The main goroutine calls Add to set the number of
// goroutines to wait for. Then each of the goroutines
// runs and calls Done when finished. At the same time,
// Wait can be used to block until all goroutines have finished.
//
// A WaitGroup must not be copied after first use.
type WaitGroup struct {
noCopy noCopy

// 64-bit value: high 32 bits are counter, low 32 bits are waiter count.
// 64-bit atomic operations require 64-bit alignment, but 32-bit
// compilers do not ensure it. So we allocate 12 bytes and then use
// the aligned 8 bytes in them as state.
state1 [12]byte
sema uint32
}

示例代码:

package main

import (
    "sync"
    "fmt"
)

func doWorker(id int, ch chan int, wg *sync.WaitGroup) {
    for n := range ch {
        fmt.Printf("Worker %d received %c\n", id, n)
        wg.Done()    // 减少一个计数
    }
}

type worker struct {
    in chan int
    wg *sync.WaitGroup
}

func createWorker(id int, wg *sync.WaitGroup) worker {
    w := worker{
        in: make(chan int),
        wg: wg,
    }

    go doWorker(id, w.in, wg)
    return w
}

func chanDemo() {
    var wg sync.WaitGroup

    var workers [10]worker

    for i:=0; i<10; i++ {
        workers[i] = createWorker(i, &wg)
    }


    for i, worker := range workers {
        wg.Add(1)      // 添加一个计数
        worker.in <- 'a' + i
    }
    wg.Wait()    // 阻塞,等待所有任务完成

}
func main() {
    chanDemo()
}

 

// A Mutex is a mutual exclusion lock.
// The zero value for a Mutex is an unlocked mutex.
//
// A Mutex must not be copied after first use.
type Mutex struct {
state int32
sema uint32
}

示例代码:

package main

import (
    "sync"
    "fmt"
)

var x = 0

func increment(wg *sync.WaitGroup, mu *sync.Mutex) {
    mu.Lock()
    x++
    mu.Unlock()
    wg.Done()
}

func main() {
    var wg sync.WaitGroup
    var mu sync.Mutex

    for i := 0; i < 1000; i++ {
        wg.Add(1)
        go increment(&wg, &mu)
    }
    wg.Wait()
    fmt.Println("final value of x", x)
}

 

// Do calls the function f if and only if Do is being called for the
// first time for this instance of Once. In other words, given
// var once Once
// if once.Do(f) is called multiple times, only the first call will invoke f,
// even if f has a different value in each invocation. A new instance of
// Once is required for each function to execute.

示例代码:
package main

import (
    "fmt"
    "sync"
)

func One () {
    fmt.Println("One")
}

func Two() {
    fmt.Println("Two")
}

func main() {
    var once sync.Once
    for i, v := range make([]string, 10) {
        once.Do(One)
        fmt.Println("count:", v, "---", i)
    }
}

执行结果:
One
count:  --- 0
count:  --- 1
count:  --- 2
count:  --- 3
count:  --- 4
count:  --- 5
count:  --- 6
count:  --- 7
count:  --- 8
count:  --- 9

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
递归(汉诺塔)Go实现发布时间:2022-07-10
下一篇:
GO_03:GO语言基础语法发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap