duandazhen7306 2011-12-02 03:53
浏览 55
已采纳

并发Go例程中的死锁错误

I have three concurrent go routines like below,

func Routine1() {
    mutex1.Lock()

    do something

    mutex2.Lock()
    mutex3.Lock()
    send int to routine 2
    send int to routine 3
   * Print Something *
    mutex2.Unlock()
    mutex3.Unlock()

    receive ints
    do something 

    mutex2.Lock()
    mutex3.Lock()
    send int to routine 2
    send int to routine 3
    Print Something
    mutex2.Unlock()
    mutex3.Unlock()

    do something
    receive ints
    mutex1.Unlock()
    wg.Done()
}

func Routine2() {
    mutex2.Lock()

    do something

    mutex1.Lock()
    mutex3.Lock()
    send int to routine 1
    send int to routine 3
    Print Something
    mutex1.Unlock()
    mutex3.Unlock()

    do something
    receive ints

    mutex1.Lock()
    mutex3.Lock()
    send int to routine 1
    send int to routine 3
    Print Something
    mutex1.Unlock()
    mutex3.Unlock()

    do something
    receive ints
    mutex2.Unlock()
    wg.Done()
}

func Routine3() {
// same structure as routine 1 & 2
}
func main() {
wg.Add(3)
go Routine1()
go Routine2()
Routine3()
wg.Wait()
}

This logical code gives deadlock situation every time. Actually, when it runs, just execute print statement of routine 1 (statement between * marks) manytimes (though my print statement is only one) and gives deadlock error. Can anybody inform me what is wrong with the code logic. Thanks.

NB. For more info, code can be found here, http://play.golang.org/p/pW6aXryUaK, where only line number 290 is executing. The original code which contains no error, can be found here play.golang.org/p/UL3rj8DJRk. I just add lock and unlock to them. Thanks.

I want to add another point: in the code of play.golang.org/p/UL3rj8DJRk, you can find outputs of various print statements like this: process [number] sends [int] to process [number] at [time nano seconds]. However, as the result of three concurrent routines, sometimes the print statement is not executing properly (means not printing whole, there is some thing inserted by another print like process [number] sends [int] to process [number] at process [number]). Can any body help me how to manage this ?

  • 写回答

1条回答 默认 最新

  • dongyanju1094 2011-12-02 08:41
    关注

    It is impossible to identify the cause of the deadlock because the code you posted does not contain enough information.

    It may be caused by the order in which you are acquiring locks, or it may be caused by usage of unbuffered Go channels.

    Until you post complete Go source code, there is no way to answer your question.

    If you need to discuss the code in greater length, you probably should post it to golang-nuts.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题