当前位置: > > > > goroutine 上的“选择性”互斥
goroutine 上的“选择性”互斥
来源:stackoverflow
2024-04-29 21:21:34
0浏览
收藏
各位小伙伴们,大家好呀!看看今天我又给各位带来了什么文章?本文标题是《goroutine 上的“选择性”互斥》,很明显是关于Golang的文章哈哈哈,其中内容主要会涉及到等等,如果能帮到你,觉得很不错的话,欢迎各位多多点评和分享!
问题内容
我是 Go 新手,我想实现一种自定义互斥机制,其中每个应用程序用户一次可以执行一个 goroutine。 为了简化起见,将 U1 和 U2 视为应用程序用户,以及 F1(userId)、F2(userId) 和 F3(userId) 三个不同的 goroutine,它们从数据库中读取/写入仅与给定用户相关的记录强>。我希望,如果U1调用(F1,F2,F3),(F2,F3)直到F1结束才执行,那么执行F2或F3(按调用顺序将是最佳解决方案),最后执行剩余的一个被执行。 U2 不受 U1 锁的影响,但她如前所述受其自己的锁控制。你会如何实施这个?是否有内置原语?谢谢
解决方案
使用 进行互斥。为每个活动用户使用一个互斥体。
我假设用户是通过某种 id 来识别的。我们将该类型称为 userid。使用由 userid 键控的映射来存储每个用户互斥锁。
只有活动用户才需要互斥体,而不是所有潜在用户。使用整数计数器来确定用户是否处于活动状态。
上述地图和计数器也应该受到互斥锁的保护。
代码如下:
type userID string // replace userID type with id type for your application
// userLock is the per user lock
type userLock struct {
sync.Mutex
// n is number of goroutines between lock and unlock
n int
}
var (
// mu protects locks map and n field in userLock
mu sync.Mutex
// locks is map of active locks
locks = map[userID]*userLock{}
)
// lockUser locks the user specified by id. If the lock is
// already in use, the calling goroutine blocks until
// the lock is available.
func lockUser(id userID) {
mu.Lock()
l := locks[id]
if l == nil {
l = &userLock{}
locks[id] = l
}
l.n++
mu.Unlock()
l.Lock()
}
// unlockUser unlocks the user specified by id. It
// is a run-time error if the user is not locked on
// entry unlockUser.
func unlockUser(id userID) {
mu.Lock()
l := locks[id]
if l == nil {
panic("unlock without lock")
}
l.n--
if l.n == 0 {
delete(locks, id)
}
mu.Unlock()
l.Unlock()
}
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《goroutine 上的“选择性”互斥》文章吧,也可关注米云公众号了解相关技术文章。
