1. Concurrency security
package main import ( "fmt" "sync" ) var ( sum int wg ) func test() { for i := 0; i < 5000000; i++ { sum += 1 } () } func main() { // Concurrency and security locks (2) go test() go test() () (sum) }
In the above code, we have enabled two goroutines to accumulate the value of the variable x. When these two goroutines access and modify x variables, there will be data competition, resulting in the final result that does not match the expected ones.
2. Mutex lock
package main import ( "fmt" "sync" ) var ( sum int wg mu // Define a mutex lock) func test() { for i := 0; i < 10000000; i++ { // Mutex lock can ensure that only one goroutine can access shared resources at the same time () sum += 1 () } () } func main() { (mu) // Concurrency and security locks (2) go test() go test() () (sum) }
Using a mutex can ensure that there is only one goroutine at the same time and only one goroutine enters the critical area, while other goroutines are waiting for the lock; when the mutex is released, the waiting goroutine can acquire the lock and enter the critical area. When multiple goroutines are waiting for a lock at the same time, the awakening strategy is random.
3. Read and write mutex lock
Mutex locks are completely mutually exclusive, but in many practical scenarios, there are more reads, fewer writes, and there is no need to add locks when we read a resource concurrently without involving resource modification. In this scenario, using reads, writes, locks are a better choice. Read and write locks use the RWMutex type in the sync package in Go language.
Read and write locks are divided into two types: read and write locks. When a goroutine acquires the read lock, other goroutines will continue to acquire the lock if they acquire the read lock, and if they acquire the write lock, they will wait; when a goroutine acquires the write lock, other goroutines will wait whether they acquire the read lock or the write lock.
package main import ( "fmt" "sync" "time" ) var ( x int wg mu // Define a mutex lock rw // Define a read and write lock. Note: Only when there are more reads and less writes, can the read and write lock play its advantages) func write() { () x += 1 (10 * ) // Assume that the write time takes 10 milliseconds () () } func read() { () () () () } func main() { start := () for i := 0; i < 10; i++ { (1) go write() } // Write time: about 160 milliseconds for i := 0; i < 1000; i++ { (1) go read() } // Reading time: about 15 milliseconds () end := () ("Execution time:", (start)) }
It should be noted that read and write locks are very suitable for scenarios where more reads and less reads. If the difference between read and write operations is not large, the advantages of read and write locks cannot be exerted.
This is the end of this article about concurrency security and locks in golang. For more related content on golang, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!