douna6802 2011-11-29 18:41
浏览 45
已采纳

并发Goroutine互斥

In my code there are three concurrent routines. I try to give a brief overview of my code,

Routine 1 {
do something

*Send int to Routine 2
Send int to Routine 3
Print Something
Print Something*

do something
}

Routine 2 {
do something

*Send int to Routine 1
Send int to Routine 3
Print Something
Print Something*

do something
}

Routine 3 {
do something

*Send int to Routine 1
Send int to Routine 2
Print Something
Print Something*

do something
}

main {
routine1
routine2
routine3
}

I want that, while codes between two do something (codes between two star marks) is executing, flow of control must not go to other goroutines. For example, when routine1 is executing the events between two stars (sending and printing events), routine 2 and 3 must be blocked (means flow of execution does not pass to routine 2 or 3 from routine 1). After completing last print event, flow of execution may pass to routine 2 or 3. Can anybody help me by specifying, how can I achieve this ? Is it possible to implement above specification by WaitGroup ? Can anybody show me by giving a simple example how to implement above specified example by using WaitGroup. Thanks.

NB: I give two sending and two printing options, in fact there is lots of sending and printing.

  • 写回答

4条回答 默认 最新

  • douchun1948 2011-11-30 00:50
    关注

    If I have gotten it correctly, what you want is to prevent simultaneous execution of some part of each function and other functions. The following code does this: fmt.Println lines won't happen as other routines are running. Here's what happens: when execution gets to the print section, it waits until other routines end, if they are running, and while this print line is executing other routines don't start and wait. I hope that is what you are looking for. Correct me if I'm wrong about this.

    package main
    
    import (
        "fmt"
        "rand"
        "sync"
    )
    
    var (
        mutex1, mutex2, mutex3 sync.Mutex
        wg sync.WaitGroup
    )
    
    func Routine1() {
        mutex1.Lock()
        // do something
        for i := 0; i < 200; i++ {
            mutex2.Lock()
            mutex3.Lock()
            fmt.Println("value of z")
            mutex2.Unlock()
            mutex3.Unlock()
        }
        // do something
        mutex1.Unlock()
        wg.Done()
    }
    
    func Routine2() {
        mutex2.Lock()
        // do something
        for i := 0; i < 200; i++ {
            mutex1.Lock()
            mutex3.Lock()
            fmt.Println("value of z")
            mutex1.Unlock()
            mutex3.Unlock()
        }
        // do something
        mutex2.Unlock()
        wg.Done()
    }
    
    func Routine3() {
        mutex3.Lock()
        // do something
        for i := 0; i < 200; i++ {
            mutex1.Lock()
            mutex2.Lock()
            fmt.Println("value of z")
            mutex1.Unlock()
            mutex2.Unlock()
        }
        // do something
        mutex3.Unlock()
        wg.Done()
    }
    
    func main() {
        wg.Add(3)
        go Routine1()
        go Routine2()
        Routine3()
        wg.Wait()
    }
    

    UPDATE: Let me explain these three mutex here: a mutex is, as documentation says: "a mutual exclusion lock." That means when you call Lock on a mutex your code just waits there if somebody else have locked the same mutex. Right after you call Unlock that blocked code will be resumed.

    Here I put each function in its own mutex by locking a mutex at the beginning of the function and unlocking it the end. By this simple mechanism you can avoid running any part of code you want at the same time as those functions. For instance, everywhere you want to have a code that should not be running when Routine1 is running, simply lock mutex1 at the beginning of that code and unlock at at the end. That is what I did in appropriate lines in Routine2 and Routine3. Hope that clarifies things.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法