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。希望能澄清一些事情。

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 !
- 3 回答
- 0 關(guān)注
- 249 瀏覽
添加回答
舉報(bào)