Golang Mutex用于锁定特定的变量/映射

huangapple go评论93阅读模式
英文:

Golang Mutex to Lock Specific Variable/Map

问题

我对Golang中的互斥锁(mutex)的工作原理有些困惑,尽管我以前用过它们。

以下是我的问题:

  1. 互斥锁(mutex)到底锁定了什么?如何只锁定特定的变量?

  2. 在使用互斥锁(mutex)时,我应该使用通道(channel)吗?

  3. 互斥锁(mutex)和锁(locker)之间有什么区别?

我正在使用Golang开发一个高并发的网站,我需要在不同的时间管理每个人的钱包,并且我试图避免程序中的任何竞争条件。例如,如果我的程序想要给一个用户添加500个积分,它会读取用户的当前余额(从Firebase中),然后加上500,最后更新该值。但是,如果由于某种原因它执行了两次,可能会对用户的钱包产生错误的更改。

英文:

I'm a little confused on how mutex's work in Golang, even though I've used them before.

Here are my questions:

  1. What exactly does a mutex lock? (How) Do you use it to lock a specific variable only?

  2. Should I use channels instead of a mutex?

  3. Is there a difference between a mutex and a locker?

I'm developing a highly concurrent website with golang, and I need to manage each person's wallet at different times and I am trying to avoid any races in my program. For example, if my program wants to add 500 credits to a user, it will read the current balance for the user (from firebase) and add 500 then update the value. But if it does this twice for some reason, there may be an incorrect change to the user's wallet.

答案1

得分: 4

互斥锁(Mutex)是一种用于并发控制的“互斥”构造。定义了互斥锁的编程语言允许程序员定义一段临界代码,只能由一个执行线程进入。这是在Go中使用互斥锁的简单示例这是一个更详细的使用互斥锁的示例

在决定在Go应用程序中使用互斥锁还是基于通道的设计时,选择并不总是非常清晰。这个来自Go GitHub仓库的维基文章给出了以下建议:

  • 通道(Channel):传递数据的所有权,分发工作单元,异步通信结果。
  • 互斥锁(Mutex):缓存,状态。

正如@JimB指出的那样,Go中的sync.Mutex类型满足sync.Locker接口类型。注意sync.Mutex具有Lock()Unlock()方法。因此,在Go中,sync.Mutex是一个sync.Locker

如果需要更多关于Go接口的帮助,可以查看这篇优秀博文

英文:

What is a Mutex?

In general, a "mutual exclusion" construct (or mutex) helps with concurrency control. Languages that define mutexes let programmers define a critical section of code that can only be entered by a single thread of execution. Here is a simple example of using mutexes in Go. Here is a bit more detailed example of using mutexes.

Mutex or Channel?

When deciding whether to use a mutex or channel based design in your Go app, the choices aren't always terribly clear. This wiki article from the Go github repo has the following advice:

Channel: passing ownership of data,
distributing units of work,
communicating async results

Mutex: caches, state

Mutex or sync.Locker?

As @JimB pointed out, the sync.Mutex type in Go satisfies the sync.Locker interface type. Notice how sync.Mutex has Lock() and Unlock() methods? So to Go, a sync.Mutex is a sync.Locker.

For more help with Go interfaces, check out this great blog post.

答案2

得分: 2

  1. Mutex只锁定自身。由您决定在代码中该锁表示什么。

  2. 使用通道在goroutine之间进行同步和通信。使用互斥锁在单个逻辑资源周围提供“互斥”。

  3. 互斥锁满足sync.Locker接口。需要sync.Locker的东西可以互换使用sync.Mutexsync.RWMutex

同步原语与防止重复事务的概念无关。正确使用同步只会防止程序中的数据竞争。

英文:
  1. A Mutex only locks itself. It's up to you to decide what that lock signifies in your code.
  2. Use channels for synchronizing and communicating between goroutines. Use a mutex to provide "mutual exclusion" around a single logical resource.
  3. A mutex satisfies the sync.Locker interface. Something that requires a sync.Locker could use a sync.Mutex or a sync.RWMutex interchangeably.

Synchronization primitives aren't related to the concept of preventing duplicate transactions. Correct use of synchronization will only prevent data races in your program.

答案3

得分: 0

如果状态不在内存中,你真的不需要同步锁。但是假设所有用户的钱包都在一个Map中,那么你就需要一个同步锁,因为并发写操作会导致竞争条件。

英文:

If the state is not in memory. You really don't need synchronous locks. But let's say all of the users wallets are inside of one Map. You would need one because concurrent writes will cause a race condition.

huangapple
  • 本文由 发表于 2017年1月3日 20:15:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/41443369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定