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.

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

报告相同问题?

悬赏问题

  • ¥50 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码