招标网站排名前十名,淘宝店铺推广渠道有哪些,线上代运营公司,网站从服务器上下载文件Golang以其并发性Goroutines而闻名。不仅是并发#xff0c;还有更多。 因此#xff0c;在这种情况下#xff0c;我们必须确保多个goroutines不应该同时试图修改资源#xff0c;从而导致冲突。 为了确保资源一次只能被一个goroutine访问#xff0c;我们可以使用一个叫做syn…Golang以其并发性Goroutines而闻名。不仅是并发还有更多。 因此在这种情况下我们必须确保多个goroutines不应该同时试图修改资源从而导致冲突。 为了确保资源一次只能被一个goroutine访问我们可以使用一个叫做sync.Mutex的东西。 This concept is called mutual exclusion, and the conventional name for the data structure that provides it is mutex. — Go dev 无Mutex的用例
让我们有一个简单的用例来理解Mutex在goroutines中的使用。 例如如果我们需要通过一个goroutine增加一个变量的值并通过另一个goroutine减少同一个变量的值。
package mainimport (fmtsynctime
)func main() {const loop 100var wg sync.WaitGroupwg.Add(loop * 2)// declaring a shared valuevar n int 0for i : 0; i loop; i {go func() {time.Sleep(time.Second / 10)nwg.Done()}()go func() {time.Sleep(time.Second / 10)n--wg.Done()}()}wg.Wait()// printing the final value of nif n ! 0 {fmt.Println(The Final value of n should be 0. But found , n)return}fmt.Printf(\nFinal value of n is %d\n\n, n) // the final of n should be 0
}
在这个循环中我使用了两个带有goroutines的匿名函数。一个将增加n的值另一个将减少n的值。在最后n的值应该是0因为初始值是0对于每个循环计数我都是先增后减所以在最后应该和初始值一样。但如果不使用Mutex情况就不是我们所期望的那样了。 在上述输出中我们可以看到结果不是恒定的。
我们可以使用go run命令中的-race来检测是否存在数据竞赛。 数据竞赛发生在一个进程中的两个或多个线程同时访问同一个内存位置。 sync.Mutex
它拥有两个方法
LockUnlock 使用Lock来锁定资源以便每次只有一个goroutine可以访问该资源。
Unlock用于解锁被锁住的资源。
使用Mutex也有同样的用例。
package mainimport (fmtsynctime
)func main() {const loop 100var wg sync.WaitGroupwg.Add(loop * 2)// declaring a shared valuevar n int 0var m sync.Mutexfor i : 0; i loop; i {go func() {time.Sleep(time.Second / 10)m.Lock() // locking the resource nnm.Unlock() // unlocking the resource nwg.Done()}()go func() {time.Sleep(time.Second / 10)m.Lock() // locking the resource nn--m.Unlock() // unlocking the resource nwg.Done()}()}wg.Wait()// printing the final value of nif n ! 0 {fmt.Println(The Final value of n should be 0. But found , n)return}fmt.Printf(\nFinal value of n is %d\n\n, n) // the final of n should be 0
}
在这里两个goroutine试图同时访问同一个资源n。但在Mutex.Lock()的帮助下我们可以锁定该资源这样它就只能被一个goroutine使用。
在上面的输出中我们可以看到输出总是0正如我们所期望的。
我们也可以在使用Mutex的时候检查数据竞赛。
我们可以清楚地看到在使用Mutex时没有数据竞赛。
另外我们可以对资源Unlock()使用defer语句所以它将在被锁定的块的末尾被解锁。
go func() {time.Sleep(time.Second / 10)m.Lock() // locking the resource nn--m.Unlock() // unlocking the resource nwg.Done()
}()