// 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 []worker for i:=; i<; i++ {
workers[i] = createWorker(i, &wg)
} for i, worker := range workers {
wg.Add() // 添加一个计数
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 = 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 := ; i < ; i++ {
wg.Add()
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, ) {
once.Do(One)
fmt.Println("count:", v, "---", i)
}
} 执行结果:
One
count: ---
count: ---
count: ---
count: ---
count: ---
count: ---
count: ---
count: ---
count: ---
count: ---

最新文章

  1. HANA SQLScript
  2. Binary search tree
  3. sql索引的填充因子多少最好,填充因子的作用?
  4. hdu Tempter of the Bone
  5. linux 安装 ftp
  6. 基于AFNetWorking封装一个网络请求数据的类
  7. Java基础之一组有用的类——使用二叉树搜索算法搜索某个作者(TryBinarySearch)
  8. chrome中tcmalloc的使用
  9. ExecuteScalar
  10. 怎样在Ubuntu中使用条件布局
  11. JPA 注解的CascadeType属性
  12. Ubuntu配置OpenStack 二:配置时间同步NTP和安装数据库Maridb以及问题总结
  13. C语言单向链表
  14. ROS_RGB-D SLAM学习笔记--室内环境测试
  15. java学习入门之---使用idea创建第一个maven项目
  16. Verilog语言实现并行(循环冗余码)CRC校验
  17. python六十六课——单元测试(二)
  18. ubuntu1604使用之旅——软件源更新(vim安装)
  19. hdu1072(Nightmare)bfs
  20. ASP.NET中Request.ApplicationPath、Request.FilePath、Request.Path、.Request.MapPath、

热门文章

  1. ios - 上下滚动的新闻
  2. Apache JMeter录制HTTPS的方法及测试中常见问题解决
  3. 【BZOJ2242】[SDOI2011]计算器 BSGS
  4. #1560 : H国的身份证号码II(dp+矩阵快速幂)
  5. 关于jquery的取消阻止默认事件
  6. 如何用SQL为每一行均产生一个随机数
  7. dataTables的导出Excel功能
  8. javafx tableview 设置多选
  9. Redis作者谈Redis应用场景(转)
  10. 关于主键(PRIMARY KEY)和自增(AUTO_INCREMENT)结合使用的知识点