douzi115522 2019-02-05 18:26
浏览 32

对goroutine的“选择性”互斥

I am new to Go and I would like to implement a custom mutual exclusion mechanism, where each application user can execute one goroutine at a time. To simplify, consider U1 and U2 as application users and F1(userId), F2(userId) and F3(userId) three different goroutines, which read/write records from a database that are related only to a given user. I would like that, if U1 calls (F1, F2, F3), (F2, F3) are not executed until F1 has ended, than F2 or F3 are executed (in calling order would be an optimal solution), lastly the remaining one is executed. U2 is not affected by U1 lock, but she is controlled by its own lock as described before. How would you implement this? Is there a built-in primitive? Thanks

  • 写回答

2条回答 默认 最新

  • drzablspw01655860 2019-02-05 18:29
    关注

    You can use a buffered channel as a semaphore, and create such a channel per user. Then two goroutines related to the same user will only be able to do things after acquiring the semaphore (successfully writing to the channel).


    Resources on using Go channels as semaphores:

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化