質問
私のコードには、3つの同時ルーチンがあります。コードの簡単な概要を説明しようとしています。
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
}
2つの間のコードが何か(2つの星のマークの間のコード)が実行されているのに対し、制御の流れは他のゴルチンに行ってはなりません。たとえば、Routine1が2つの星間のイベント(送信および印刷イベント)を実行している場合、ルーチン2と3をブロックする必要があります(実行の流れは、ルーチン1からルーチン2または3に渡されません)。最後の印刷イベントを完了した後、実行のフローはルーチン2または3に渡る場合があります。上記の仕様を実装することは可能ですか? WATGROUP ? WaitGroupを使用して、上記の例を実装する方法を簡単な例を挙げることで、誰かが私に見せることができます。ありがとう。
NB:私は2つの送信と2つの印刷オプションを提供します。実際、多くの送信と印刷があります。
解決
私がそれを正しく取得した場合、あなたが望むのは、各関数の一部と他の機能の一部の同時実行を防ぐことです。次のコードはこれを行います: fmt.Println
他のルーチンが実行されているため、ラインは発生しません。ここで何が起こりますか:実行が印刷セクションに到達すると、他のルーチンが終了している場合、それらが実行されている場合、そしてこの印刷ラインが実行されている間、他のルーチンを実行している間、それは開始されず、待機しません。それがあなたが探しているものであることを願っています。私がこれについて間違っているなら私を修正してください。
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()
}
アップデート: :ここでこれらの3つのミューテックスを説明させてください:ミューテックスは、 ドキュメントは言っています: :「相互除外ロック」。それはあなたが電話するときを意味します Lock
Mutexでは、他の誰かが同じミューテックスをロックした場合、コードはそこで待機します。電話した直後 Unlock
そのブロックされたコードが再開されます。
ここでは、関数の先頭にミューテックスをロックし、終了時にロックを解除することにより、各関数を独自のミューテックスに配置します。この単純なメカニズムにより、必要なコードの部分をそれらの関数と同時に実行することを避けることができます。たとえば、どこにでも実行してはならないコードがあります。 Routine1
実行中、単にロックしています mutex1
そのコードの開始時に、最後にロックを解除します。それが私が適切な行でしたことです Routine2
と Routine3
. 。それが物事を明確にすることを願っています。
他のヒント
使用できます sync.Mutex
. 。たとえば、間のすべて im.Lock()
と im.Unlock()
他のゴルウチンを除外します。
package main
import (
"fmt"
"sync"
)
func f1(wg *sync.WaitGroup, im *sync.Mutex, i *int) {
for {
im.Lock()
(*i)++
fmt.Printf("Go1: %d\n", *i)
done := *i >= 10
im.Unlock()
if done {
break
}
}
wg.Done()
}
func f2(wg *sync.WaitGroup, im *sync.Mutex, i *int) {
for {
im.Lock()
(*i)++
fmt.Printf("Go2: %d\n", *i)
done := *i >= 10
im.Unlock()
if done {
break
}
}
wg.Done()
}
func main() {
var wg sync.WaitGroup
var im sync.Mutex
var i int
wg.Add(2)
go f1(&wg, &im, &i)
go f2(&wg, &im, &i)
wg.Wait()
}
もう1つの方法は、コントロールチャネルを持つことです。一度にゴルチンが1つだけ実行されることがあり、各ルーチンはアトミック操作を完了するたびに「コントロールロック」に戻ります。
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 !")
}
これは印刷:
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 !
他のルーチンを明示的に停止するライブラリ機能を求めていますか?他のほとんどの言語でも、GOや他のほとんどの言語でも不可能であることを明確にしてください。同期すると、Mutexケースはあなたのケースでは不可能です。