This is a created article in which the information may have evolved or changed.
It is well known that the support of the Go language in multithreading is very complete. A cond class is provided in the Go Language Sync package, which is used for collaboration between Goroutine.
This class is not complex, only three functions, broadcast (), Signal (), Wait (), a member variable, L Lock
One of the functions implemented by broadcast () is to wake up all goroutine waiting on this cond, while signal () selects only one to wake. Wait () Nature is to let Goroutine in
The cond on the waiting. These functions have several points to note:
The 1.Wait () function must ensure that it has obtained its member variable lock L when called, because the first thing to do is unlock. However, it is important to note that when wait () finishes waiting to return, the
It will re-lock the L, which means that when wait ends, the goroutine that calls it still gets lock L.
2. Calling the broadcast () function causes the system to switch to the goroutine that was previously waiting for execution.