Mutual Exclusion of Concurrent Goroutines

后端 未结 4 2121
天命终不由人
天命终不由人 2020-12-18 13:56

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 Routi         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-18 14:31

    Another way would be to have a control channel, with only one goroutine allowed to execute at any one time, with each routine sending back to the 'control lock' whenever they are done doing their atomic operation:

    package main
    import "fmt"
    import "time"
    
    func routine(id int, control chan struct{}){
        for {
            // Get the control
            <-control
            fmt.Printf("routine %d got control\n", id)
            fmt.Printf("A lot of things happen here...")
            time.Sleep(1)
            fmt.Printf("... but only in routine %d !\n", id)
            fmt.Printf("routine %d gives back control\n", id)
            // Sending back the control to whichever other routine catches it
            control<-struct{}{}
        }
    }
    
    func main() {
        // Control channel is blocking
        control := make(chan struct{})
    
        // Start all routines
        go routine(0, control)
        go routine(1, control)
        go routine(2, control)
    
        // Sending control to whichever catches it first
        control<-struct{}{}
        // Let routines play for some time...
        time.Sleep(10)
        // Getting control back and terminating
        <-control
        close(control)
        fmt.Println("Finished !")
    }
    

    This prints:

    routine 0 got control
    A lot of things happen here...... but only in routine 0 !
    routine 0 gives back control
    routine 1 got control
    A lot of things happen here...... but only in routine 1 !
    routine 1 gives back control
    routine 2 got control
    A lot of things happen here...... but only in routine 2 !
    routine 2 gives back control
    routine 0 got control
    A lot of things happen here...... but only in routine 0 !
    routine 0 gives back control
    routine 1 got control
    A lot of things happen here...... but only in routine 1 !
    routine 1 gives back control
    routine 2 got control
    A lot of things happen here...... but only in routine 2 !
    routine 2 gives back control
    routine 0 got control
    A lot of things happen here...... but only in routine 0 !
    routine 0 gives back control
    routine 1 got control
    A lot of things happen here...... but only in routine 1 !
    routine 1 gives back control
    routine 2 got control
    A lot of things happen here...... but only in routine 2 !
    routine 2 gives back control
    routine 0 got control
    A lot of things happen here...... but only in routine 0 !
    routine 0 gives back control
    routine 1 got control
    A lot of things happen here...... but only in routine 1 !
    routine 1 gives back control
    routine 2 got control
    A lot of things happen here...... but only in routine 2 !
    routine 2 gives back control
    Finished !
    

提交回复
热议问题