我有三个并发去例程下面喜欢,

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()
}

此逻辑代码给出死锁情况每一次。实际上,它运行时,只执行常规1(*标记之间的语句)manytimes(虽然我的print语句只有一个),并给出了死锁错误的打印语句。任何人都可以告诉我什么是错的代码逻辑。感谢。

NB。欲了解更多信息,代码可以在这里找到, http://play.golang.org/p/pW6aXryUaK,其中只有行数290正在执行。其中不包含错误的原代码,可以在这里找到play.golang.org/p/UL3rj8DJRk。我只是添加锁定和解锁他们。感谢。

<强>我要添加另一个点:在play.golang.org/p/UL3rj8DJRk的代码,就可以找到这样的各种打印语句的输出:过程[数字]发送[INT]至过程[数]在[时间纳米秒]。然而,由于三个并发例程,有时打印语句未正确执行(装置不打印整个结果,存在由相同工艺的另一打印插入一些事情[数字]在过程发送[INT]至过程[数字] [数])。任何机构可以帮助我如何管理呢?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top