golang 同步

Golang 中的同步原语有很多种,包括 Mutex、RWMutex、WaitGroup、Once 和 Cond 等。Mutex 是最常用的一种,它允许在共享资源上互斥访问(不能同时访问)。

同步原语是Golang中实现高效率并发编程的基础,它们提供了一种在多个goroutine之间进行通信和同步的方法,本文将详细介绍Golang中的同步原语,包括互斥锁、读写锁、通道和原子操作等,以及如何使用这些同步原语来编写高效的并发程序。

互斥锁(Mutex)

互斥锁是一种简单的同步原语,用于保护共享资源的访问,当一个goroutine获得互斥锁时,其他goroutine将被阻塞,直到锁被释放,互斥锁的使用场景包括:保护共享数据结构、限制同时访问的goroutine数量等。

golang 同步

下面是一个使用互斥锁的例子:

package main
import (
 "fmt"
 "sync/mutex"
 "time"
)
var counter int
var mu sync.Mutex
func worker() {
 for i := 0; i < 1000; i++ {
  mu.Lock()
  counter++
  mu.Unlock()
  time.Sleep(1 * time.Millisecond)
 }
}
func main() {
 go func() {
  for i := 0; i < 1000; i++ {
   mu.Lock()
   counter++
   mu.Unlock()
   time.Sleep(1 * time.Millisecond)
  }
 }()
 for i := 0; i < 2; i++ {
  go worker()
 }
 time.Sleep(1 * time.Second)
 fmt.Println("Counter:", counter)
}

读写锁(Read-Write Lock)

读写锁允许多个goroutine同时读取共享资源,但只允许一个goroutine写入,这使得读操作比写操作更高效,读写锁的使用场景包括:读多写少的场景、缓存系统等。

golang 同步

下面是一个使用读写锁的例子:

package main
import (
 "fmt"
 "sync/RWMutex"
 "time"
)
var counter int64
var mtx = &RWMutex{}
var rwl = new(sync.RWMutex) // read lock is a wrapper of the write lock, so we can use it directly here.
var writers int64 // number of writers currently holding the lock. This is used to avoid deadlocks when no one is writing. If this value becomes zero and there are readers waiting, then the writer should release the lock and allow some of them to proceed. We do that by decrementing the counter and checking if it's zero. If so, we unlock and call the next reader or writer to acquire the lock. Otherwise, we just wait for someone else to release it. This way we ensure that no one is stuck waiting for the lock forever. The downside is that this approach introduces some overhead because we have to check whether there are any writers or not before allowing a reader to acquire the lock. However, this overhead is usually negligible compared to the benefit of having multiple readers working concurrently without blocking each other. The advantage of using a read lock is that you don't need to worry about deadlocks as much as with a write lock since you can always release your read lock and reacquire it later without affecting the other goroutines. Also, you can have multiple readers at the same time which makes sense in many cases where you want to serve multiple clients simultaneously but only one needs to update the data (the writer). In such cases, you can have several readers reading from the same data structure while only one writer updating it. This reduces contention and improves performance. When all readers finish their work they can release their read locks and let other readers start working on the data again. This allows you to achieve high concurrency without having to use a write lock. You can also use a write-read lock if you need both read and write operations on a single resource but still want to optimize for reads more than writes. In this case you would use a read lock for most of the time and a write lock only when you need to modify the data. This way you can reduce contention and improve performance for reads while still allowing concurrent writes if necessary.

原创文章,作者:酷盾叔,如若转载,请注明出处:https://www.kdun.com/ask/151971.html

本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。

(0)
酷盾叔
上一篇 2024-01-17 02:55
下一篇 2024-01-17 02:57

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

产品购买 QQ咨询 微信咨询 SEO优化
分享本页
返回顶部
云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购 >>点击进入