Application of synchronization/asynchronous and locking in Go~~sync package

Implementation of lock in Go~~sync package

Mutex locks are provided in the sync package in go;

In the previous channel article in Go, we used the time.Sleep() function to make the Goroutine of the main function block until all coroutines finish. However, this is not a good method because we cannot accurately know the coroutines in actual applications. When will it end (a series of factors such as server performance, network fluctuations, and IO should be considered;

WaitGroup is provided in the sysn package to realize coordination between coroutines;

Sync waiting group

Synchronous sync and asynchronous sync;

The synchronization waiting group WaitGroup is provided in go
to see the source code:

//等待一组Goroutine完成; 阻塞的直至所有的goroutine完成;
// 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.
//

Guess you like

Origin blog.csdn.net/weixin_54061333/article/details/130988482