第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

并發(fā)goroutine的互斥

并發(fā)goroutine的互斥

Go
子衿沉夜 2021-05-05 09:21:24
在我的代碼中,有三個并發(fā)例程。我嘗試簡要概述一下我的代碼,Routine 1 {do something*Send int to Routine 2Send int to Routine 3Print SomethingPrint Something*do something}Routine 2 {do something*Send int to Routine 1Send int to Routine 3Print SomethingPrint Something*do something}Routine 3 {do something*Send int to Routine 1Send int to Routine 2Print SomethingPrint Something*do something}main {routine1routine2routine3}我希望在執(zhí)行兩個代碼之間的某些操作(兩個星號之間的代碼)時,控制流一定不能進(jìn)入其他goroutine。例如,當(dāng)例程1正在執(zhí)行兩顆星之間的事件(發(fā)送和打印事件)時,例程2和3必須被阻止(執(zhí)行的平均流量不會從例程1傳遞到例程2或3)。在完成上一個打印事件后,執(zhí)行流程可能會傳遞到例程2或3。有人可以通過指定幫助我的方式,我該如何實(shí)現(xiàn)呢?是否可以通過WaitGroup來實(shí)現(xiàn)上述規(guī)范?任何人都可以通過舉一個簡單的示例向我展示如何使用WaitGroup來實(shí)現(xiàn)上面指定的示例。謝謝。注意:我提供兩個發(fā)送和兩個打印選項(xiàng),實(shí)際上有很多發(fā)送和打印功能。
查看完整描述

3 回答

?
瀟湘沐

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個贊

如果我正確理解了它,那么您想要的是防止同時執(zhí)行每個功能的某些部分和其他功能。下面的代碼可以做到這一點(diǎn):fmt.Println當(dāng)其他例程正在運(yùn)行時,行不會發(fā)生。這是發(fā)生的情況:當(dāng)執(zhí)行到達(dá)打印部分時,它將等待直到其他例程結(jié)束(如果它們正在運(yùn)行),并且在執(zhí)行此打印行時,其他例程不會啟動并等待。我希望這就是您想要的。如果我對此有誤,請糾正我。


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

}

更新:讓我在這里解釋這三個互斥鎖:一個互斥鎖,如文檔所述:“互斥鎖”。這意味著當(dāng)您調(diào)用Lock互斥鎖時,如果其他人已鎖定相同的互斥鎖,則代碼僅在此處等待。在您致電之后Unlock,被阻止的代碼將立即恢復(fù)。


在這里,我通過在函數(shù)的開頭鎖定互斥鎖并將其解鎖來將每個函數(shù)置于其自己的互斥鎖中。通過這種簡單的機(jī)制,您可以避免與這些功能同時運(yùn)行所需的任何代碼部分。例如,在您想要擁有運(yùn)行時不應(yīng)該運(yùn)行的代碼的任何地方Routine1,只需mutex1在該代碼的開頭鎖定并在最后解鎖即可。這就是我在Routine2和中的適當(dāng)行中所做的事情Routine3。希望能澄清一些事情。


查看完整回答
反對 回復(fù) 2021-05-10
?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個贊

另一種方法是擁有一個控制通道,在任何時候只允許執(zhí)行一個goroutine,并且每當(dāng)它們完成其原子操作時,每個例程就會發(fā)回“ control lock”:


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 !")

}

打?。?/p>


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 !


查看完整回答
反對 回復(fù) 2021-05-10
  • 3 回答
  • 0 關(guān)注
  • 249 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號