1. How to make the main goroutine wait for other goroutines
Method 1: Let the main goroutine "sleep" for a period of time
func main() { for i := 0; i < 10; i++ { go func(i int) { (i) }(i) } ( * 100) }
The disadvantage is: it is difficult to grasp the time of sleep.
Method 2: Use channel, usechan struct{}
type
func main() { num := 10 var ch = make(chan struct{}, num) for i := 0; i < num; i++ { go func(i int) { (i) ch <- struct{}{} }(i) } for i := 0; i < num; i++ { <-ch } }
Type literalstruct{}
Some are similar to empty interface typesinterface{}
, it represents an empty structure type that contains no fields and does not own any methods.
struct{}
There is only one representation of type values, i.e.:struct{}{}
. And the memory space it takes is 0 bytes. To be precise, this value will always exist in the entire GO program.
Method 3: Use
To be supplemented.
2. How to make multiple goroutines run in a predetermined order
package main import ( "fmt" "sync/atomic" "time" ) func main() { num := uint32(100) var count uint32 = 0 trigger := func(i uint32, fn func()) { for { if atomic.LoadUint32(&count) == i { fn() atomic.AddUint32(&count, 1) break } // It is necessary to add Sleep statement here () } } for i := uint32(0); i < num; i++ { go func(i uint32) { fn := func() { (i) } trigger(i, fn) }(i) } trigger(num, func() {}) }
The trigger function here implements a kind of spin.
Added to the spin above()
Statement:
This is mainly because: the Go scheduler will only notify the running goroutine when needed, trying to get it to stop. However, it will not and cannot force a goroutine to stop.
Therefore, if a for statement is too simple, such as the for statement here is very simple (because there is only one if statement), then the current goroutine may not respond normally (or there is no chance to respond) to the Go scheduler stop notification.
Therefore, adding a sleep here is to: in any case (such as any version of Go, Go under any computing platform, any number of CPU cores, etc.), these goroutines containing this for statement can respond to stop notifications normally.
Without adding Sleep statements, it may cause the resource to be seized and there will be no chance to run, which may cause the program to be run and not terminated.
Optimistic lock: Always assume that there is no "other" competing operation during the process of "I" operating shared resources. If you find that "other people" are indeed competing during this period, that is, you find that the assumption fails, then wait and then operate. CAS atomic operation can basically reflect this idea. Generally, low-frequency concurrent operations are suitable for optimism locking. Optimistic locks generally use lighter synchronization methods (such as atomic operations), but not 100%. Note that high-frequency operation with optimistic locking may affect performance, because there is an additional step of "finding whether someone competes with me" operation (of course, standard CAS operations can minimize this impact).
Pessimistic lock: It is always assumed that there must be "other people" competing operations in the process of "I" operating shared resources. So "I" will first use some synchronization method (such as a mutex lock) to protect my operations. In this way, there is no need to check whether someone is competing with me when I am about to operate (because "I" always assume that there must be competition and has been protected). Generally, concurrent operations with higher frequency are suitable for pessimistic locking. However, if the frequency of concurrent operations is very low, it is also possible to use pessimistic locks, because in this case there is little impact on performance.
Finally, it is important to note that when using any synchronous method and asynchronous method, the correctness of the program must be considered first and the performance of the program must be considered. The correctness of the program must be guaranteed by functional testing, and the performance of the program must be guaranteed by performance testing.
This is the end of this article about how to use Go routine. For more related Go routine content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!