For-learning-Go-Tutorial
Go语言是谷歌2009发布的第二款开源编程语言。
Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全、支持并行进程。
因而一直想的是自己可以根据自己学习和使用Go语言编程的心得,写一本Go的书可以帮助想要学习Go语言的初学者快速入门开发和使用!
Sync.WaitGroup解析
Golang中的同步可以通过Sync.WaitGroup来实现的.WaitGroup的功能,它实现了一个类似队列的结构,可以一直向队列中添加任务,当任务完成后便从队列中删除,如果队列中的任务没有完全完成,可以通过Wait()函数来出发阻塞,防止程序继续进行,直到所有的队列任务都完成为止.
WaitGroup的特点是,Wait()方法可以用来阻塞直到队列中的所有任务都完成时才解除阻塞,而不需要sleep一个固定的时间来等待.但是其缺点是无法指定固定的Goroutine数目,我们可以通过使用channel解决这个问题。
Sync.WaitGroup中有3个方法,Add(),Done(),Wait()。其中Done()是Add(-1)的别名。
Sync.WaitGroup中三个方法的作用是:
- Add:添加或者减少等待goroutine的数量
- Done:相当于Add(-1),减掉一个goroutine计数,计数不为0
- Wait:执行阻塞,直到所有的WaitGroup数量变成0
这里需要注意下,Sync中的Add()方法和Done方法,官方的文档也有说明这个,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.即在运行main函数的goroutine里运行Add()方法,在其他的goroutine里面运行Done()函数。
Add()方法添加将可能为负的增量添加到WaitGroup计数器。如果计数器变为零,则释放在等待时阻止的所有goroutine。如果计数器变为负数,则panic。 请注意,在计数器为零时发生的具有正增量的调用时必须在Wait()方法之前。 具有负增量的调用或具有在计数器大于零时开始的正增量的调用可以在任何时间发生。 通常,这意味着对Add的调用应该在语句之前执行,创建要等待的goroutine或其他事件。如果重新使用WaitGroup等待几个独立的事件集,新的Add()调用必须在所有先前的Wait()调用返回后发生。
func (wg *WaitGroup) Add(delta int) {statep, semap := wg.state()if race.Enabled {_ = *statep // trigger nil deref earlyif delta < 0 {// Synchronize decrements with Wait.race.ReleaseMerge(unsafe.Pointer(wg))}race.Disable()defer race.Enable()}state := atomic.AddUint64(statep, uint64(delta)<<32)v := int32(state >> 32)w := uint32(state)if race.Enabled && delta > 0 && v == int32(delta) {// The first increment must be synchronized with Wait.// Need to model this as a read, because there can be// several concurrent wg.counter transitions from 0.race.Read(unsafe.Pointer(semap))}if v < 0 {panic("sync: negative WaitGroup counter")}if w != 0 && delta > 0 && v == int32(delta) {panic("sync: WaitGroup misuse: Add called concurrently with Wait")}if v > 0 || w == 0 {return}// This goroutine has set counter to 0 when waiters > 0.// Now there can't be concurrent mutations of state:// - Adds must not happen concurrently with Wait,// - Wait does not increment waiters if it sees counter == 0.// Still do a cheap sanity check to detect WaitGroup misuse.if *statep != state {panic("sync: WaitGroup misuse: Add called concurrently with Wait")}// Reset waiters count to 0.*statep = 0for ; w != 0; w-- {runtime_Semrelease(semap, false)}}
Done()方法将WaitGroup计数器减1。
func (wg *WaitGroup) Done() {wg.Add(-1)}
Wait()直到WaitGroup计数器为零。
func (wg *WaitGroup) Wait() {statep, semap := wg.state()if race.Enabled {_ = *statep // trigger nil deref earlyrace.Disable()}for {state := atomic.LoadUint64(statep)v := int32(state >> 32)w := uint32(state)if v == 0 {// Counter is 0, no need to wait.if race.Enabled {race.Enable()race.Acquire(unsafe.Pointer(wg))}return}// Increment waiters count.if atomic.CompareAndSwapUint64(statep, state, state+1) {if race.Enabled && w == 0 {// Wait must be synchronized with the first Add.// Need to model this is as a write to race with the read in Add.// As a consequence, can do the write only for the first waiter,// otherwise concurrent Waits will race with each other.race.Write(unsafe.Pointer(semap))}runtime_Semacquire(semap)if *statep != 0 {panic("sync: WaitGroup is reused before previous Wait has returned")}if race.Enabled {race.Enable()race.Acquire(unsafe.Pointer(wg))}return}}}
应用示例:
func main(){var wg sync.WaitGroupfor i:=0;i<5;i=i+1{wg.Add(1)go func(n int) {//defer wg.Done(),注意这个Done的位置,是另一个函数defer wg.Add(-1)EchoNumber(n)}(i)}wg.Wait()}func EchoNumber(i int){time.Sleep(time.Millisecond *2000)fmt.Println(i)}
运行:
34201
这个应用示例很简单,是将每次循环的数量过3秒钟输出。那么,这个程序如果不用WaitGroup,那么将看不见输出结果。因为Goroutine还没执行完,主线程已经执行完毕。注释的defer wg.Done()和defer wg.Add(-1)作用一样。