Skip to content

Commit d42a488

Browse files
kevinburkegopherbot
authored andcommitted
sync: add more notes about Cond behavior
Cond is difficult to use correctly (I was just bitten by it in a production app that I inherited). While several proposals have come up to improve or remove sync.Cond, no action has so far been taken. Update the documentation to discourage use of sync.Cond, and point people in the direction of preferred alternatives. I believe this will help encourage behavior we want (less use of sync.Cond and more use of channels), while also paving the way for, potentially, removing Cond in a future version of the language. Thanks very much to Bryan Mills and Sean Liao for discussion and recommendations. Updates #20491. Updates #21165. Change-Id: Ib4d0631c79d4c4d0a30027255cd43bc47cddebd3 Reviewed-on: https://go-review.googlesource.com/c/go/+/412237 Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 9e2f289 commit d42a488

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/sync/cond.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ import (
2222
// In the terminology of the Go memory model, Cond arranges that
2323
// a call to Broadcast or Signal “synchronizes before” any Wait call
2424
// that it unblocks.
25+
//
26+
// For many simple use cases, users will be better off using channels than a
27+
// Cond (Broadcast corresponds to closing a channel, and Signal corresponds to
28+
// sending on a channel).
29+
//
30+
// For more on replacements for sync.Cond, see [Roberto Clapis's series on
31+
// advanced concurrency patterns], as well as [Bryan Mills's talk on concurrency
32+
// patterns].
33+
//
34+
// [Roberto Clapis's series on advanced concurrency patterns]: https://blogtitle.github.io/categories/concurrency/
35+
// [Bryan Mills's talk on concurrency patterns]: https://drive.google.com/file/d/1nPdvhB0PutEJzdCq5ms6UI58dp50fcAN/view
2536
type Cond struct {
2637
noCopy noCopy
2738

@@ -64,6 +75,9 @@ func (c *Cond) Wait() {
6475
//
6576
// It is allowed but not required for the caller to hold c.L
6677
// during the call.
78+
//
79+
// Signal() does not affect goroutine scheduling priority; if other goroutines
80+
// are attempting to lock c.L, they may be awoken before a "waiting" goroutine.
6781
func (c *Cond) Signal() {
6882
c.checker.check()
6983
runtime_notifyListNotifyOne(&c.notify)

0 commit comments

Comments
 (0)