【问题标题】:"selective" mutual exclusion on goroutinesgoroutine 上的“选择性”互斥
【发布时间】:2019-02-05 18:26:48
【问题描述】:

我是 Go 新手,我想实现一种自定义互斥机制,每个应用程序用户一次可以执行一个 goroutine。 为简化起见,将 U1 和 U2 视为应用程序用户,以及 F1(userId)、F2(userId) 和 F3(userId) 三个不同的 goroutine,它们从数据库中读取/写入仅与给定用户相关的记录强>。我希望这样,如果 U1 调用 (F1, F2, F3), (F2, F3) 直到 F1 结束才执行,则执行 F2 或 F3 (按调用顺序将是最佳解决方案),最后是剩余的被执行。 U2 不受 U1 锁的影响,但如前所述,她由自己的锁控制。你将如何实现这一点?有内置的原语吗?谢谢

【问题讨论】:

标签: go concurrency synchronization locking mutex


【解决方案1】:

使用sync.Mutex 进行互斥。为每个活动用户使用一个互斥锁。

我假设用户是通过某种 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()
}

【讨论】:

    【解决方案2】:

    您可以将缓冲通道用作信号量,并为每个用户创建这样的通道。那么同一个用户相关的两个 goroutine 只能在获取信号量(成功写入通道)后才能做事。


    使用 Go 通道作为信号量的资源:

    【讨论】:

    • 感谢您的建议,我会研究链接资源。这种方法可以扩展吗?
    • @user3061631:规模在什么意义上?
    猜你喜欢
    • 2011-01-12
    • 2020-11-07
    • 2011-09-24
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 2018-11-25
    • 2016-01-05
    相关资源
    最近更新 更多