Go WaitGroup流程

WaitGroup流程

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, and the other 4 as storage
	// for the sema.
	state1 [3]uint32
}

等效于:

type WaitGroup struct {
    counter
    awaitCount
    semap
}

Add 流程

flowchart LR ST("Add(delta int)") --> B[counter++] B--"counter<0"-->P("panic") B--"awaitCount>0 and delta>0"-->P B--"counter>0 or awaitCount==0"-->ED B--"counter<0 and awaitCount>0"-->C["runtime_Semrelease(semap)"]-->ED ED(End)

Done 流程

func (wg *WaitGroup) Done() {
	wg.Add(-1)
}

Wait 流程

flowchart LR ST("Wait()") ST--"counter=0"-->ED ST--"counter>0"-->A["awaitCount++"]-->B["runtime_Semacquire(semap)"]-->ED ED(End)
posted @ 2024-06-24 09:37  Aloe_n  阅读(1)  评论(0编辑  收藏  举报