質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

Q&A

解決済

1回答

826閲覧

Go言語のsync.Condの挙動が分かりません。

退会済みユーザー

退会済みユーザー

総合スコア0

Go

Go(golang)は、Googleで開発されたオープンソースのプログラミング言語です。

0グッド

0クリップ

投稿2020/07/25 22:06

編集2020/07/26 02:27

該当のソースコード

GitHubのソースコード

発生している問題・エラーメッセージ

期待する結果は以下の通りなのですが、

{"success":true,"id":1} {"success":true,"id":2} {"success":true,"id":3} {"success":false,"id":4} {"success":true,"id":5} {"success":false,"id":6} {"id":7,"feed":[{"body":"Another post to add","timestamp":43242421}]}

以下のコマンドで2つのサブスレッドを発生させると(一つ目のコマンド引数がスレッドの数です。)、デッドロックを起こしてしまいます。

$ twitter kazukiegusa$ go run twitter.go 2 4 < tasks.json {"success":true,"id":1} {"success":true,"id":2} {"success":true,"id":3} {"success":false,"id":4} {"success":false,"id":6} {"id":7,"feed":[{"body":"Another post to add","timestamp":43242421}]} fatal error: all goroutines are asleep - deadlock! goroutine 1 [semacquire]: sync.runtime_Semacquire(0xc000016088) /usr/local/go/src/runtime/sema.go:56 +0x42 sync.(*WaitGroup).Wait(0xc000016080) /usr/local/go/src/sync/waitgroup.go:130 +0x64 main.main() /Users/kazukiegusa/mpcs/Parallel Programming/egusa/proj1/twitter/twitter.go:66 +0x422 exit status 2

実現したいこと&わからないこと

質問の箇所は、twitter.go内にあります。まず、main関数の中で、コンディション変数を定義しています。

Go

1・・・中略・・・ 2 3var wg sync.WaitGroup 4 5cond := sync.NewCond(new(sync.Mutex)) // Conditional variable 6 7for i := 0; i < numGoroutines; i++ { 8 wg.Add(1) 9 go consumer(q, f, blockSize, cond, &wg) 10} 11 12producer(q, cond) 13 14// 全てのサブスレッドがconsumer関数の実行を終えるまで待つ 15wg.Wait()

producer関数の中では、エンキュー(Enqueue)するごとに、sync.CondSignal関数を使って、consumer関数内で待機しているスレッドを一つ呼び起こしています。

func producer(q *queue.Queue, cond *sync.Cond) { ・・・中略・・・ for { ・・・中略(ここにエンキュー(Enqueue)を実行するコードが入ります。)・・・ // 待機中のサブスレッドを一つ呼び起こす cond.Signal() } }

consumer関数内では、初期化するときにsync.Condに紐付けたMutexを使ってロックしています。

func consumer(q *queue.Queue, f feed.Feed, blockSize int64, cond *sync.Cond, wg *sync.WaitGroup) { cond.L.Lock() // キューが空なら待機する cond.Wait() ・・・中略(ここにデキュー(Dequeue)を実行するコードが入ります。)・・・ cond.L.Unlock() wg.Done() }

試したこと

いろいろ試しましたが、sync.Condの挙動がきちんと把握できていないのかなと思っています。

よろしくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

nobonobo

2020/07/26 00:40 編集

タイトル通りの質問内容にまとめて欲しい・・・。 sync.Condにどのような挙動を期待していて試してみたことに絞って質問してもらえれば回答がつきやすいと思います。
退会済みユーザー

退会済みユーザー

2020/07/26 02:28

ご指摘ありがとうございます!質問を修正してみました。 コードも多少変えています。どうやらデッドロックが起きてしまっているみたいです。
guest

回答1

0

ベストアンサー

sync.Condの使い方は https://golang.org/pkg/sync/#Cond.Wait

その他の問題はいろいろ指摘が多岐にわたってしまうのでdiffだけ提供します。
(というか質問の際に抜粋等してもらえると回答しやすいです)

diff

1diff --git a/queue/queue.go b/queue/queue.go 2index d8b1c50..2af95b7 100644 3--- a/queue/queue.go 4+++ b/queue/queue.go 5@@ -27,6 +27,11 @@ func NewQueue() (q *Queue) { 6 return 7 } 8 9+// IsEmpty ... 10+func (q *Queue) IsEmpty() bool { 11+ return q.head == q.tail 12+} 13+ 14 // LoadPointer atomically loads *addr. 15 // https://golang.org/pkg/sync/atomic/#LoadPointer 16 func load(p *unsafe.Pointer) (n *node) { 17diff --git a/twitter/twitter.go b/twitter/twitter.go 18index 242a0f6..c9c81aa 100644 19--- a/twitter/twitter.go 20+++ b/twitter/twitter.go 21@@ -47,7 +47,7 @@ func main() { 22 if len(args) == 0 { 23 blockSize := int64(math.MaxInt64) 24 producer(q, nil) 25- consumer(q, f, blockSize, nil, &wg) 26+ consumer(q, f, blockSize, nil) 27 28 } else { 29 numGoroutines, _ := strconv.Atoi(args[0]) // Number of goroutines to spawn 30@@ -57,7 +57,10 @@ func main() { 31 32 for i := 0; i < numGoroutines; i++ { 33 wg.Add(1) 34- go consumer(q, f, blockSize, cond, &wg) 35+ go func() { 36+ defer wg.Done() 37+ consumer(q, f, blockSize, cond) 38+ }() 39 } 40 41 producer(q, cond) 42@@ -93,13 +96,14 @@ func producer(q *queue.Queue, cond *sync.Cond) { 43 } 44 45 // consumer grabs tasks from the queue and starts to execute them. 46-func consumer(q *queue.Queue, f feed.Feed, blockSize int64, cond *sync.Cond, wg *sync.WaitGroup) { 47+func consumer(q *queue.Queue, f feed.Feed, blockSize int64, cond *sync.Cond) { 48 49 if cond != nil { 50 cond.L.Lock() 51- 52- // Go to sleep if the queue is empty 53- cond.Wait() 54+ for q.IsEmpty() { 55+ cond.Wait() 56+ } 57+ cond.L.Unlock() 58 } 59 60 // Dequeue the task 61@@ -132,9 +136,8 @@ func consumer(q *queue.Queue, f feed.Feed, blockSize int64, cond *sync.Cond, wg 62 } 63 64 if cond != nil { 65- cond.L.Unlock() 66+ //cond.L.Unlock() 67 } 68- wg.Done() 69 } 70 71 // response execute the task. 72

投稿2020/07/26 07:50

nobonobo

総合スコア3367

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

nobonobo

2020/07/26 23:42

質問のリポジトリを消されると僕の回答も無意味になってしまいますね。今後は質問されるときはここに書かれた情報で完結するような質問にまとめていただけると助かります!
退会済みユーザー

退会済みユーザー

2020/07/29 15:49

すいません、かしこまりました!次回から消さないようにします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問