This is an excerpt of the post; the full post is available here: https://victoriametrics.com/blog/go-sync-cond/
This post is part of a series about handling concurrency in Go:
In Go, sync.Cond is a synchronization primitive, though it's not as commonly used as its siblings like sync.Mutex or sync.WaitGroup. You'll rarely see it in most projects or even in the standard libraries, where other sync mechanisms tend to take its place.
That said, as a Go engineer, you don't really want to find yourself reading through code that uses sync.Cond and not have a clue what's going on, because it is part of the standard library, after all.
So, this discussion will help you close that gap, and even better, it'll give you a clearer sense of how it actually works in practice.
So, let's break down what sync.Cond is all about.
When a goroutine needs to wait for something specific to happen, like some shared data changing, it can "block," meaning it just pauses its work until it gets the go-ahead to continue. The most basic way to do this is with a loop, maybe even adding a time.Sleep to prevent the CPU from going crazy with busy-waiting.
Here's what that might look like:
// wait until condition is true for !condition { } // or for !condition { time.Sleep(100 * time.Millisecond) }
Now, this isn't really efficient as that loop is still running in the background, burning through CPU cycles, even when nothing's changed.
That's where sync.Cond steps in, a better way to let goroutines coordinate their work. Technically, it's a "condition variable" if you're coming from a more academic background.
Here's the basic interface sync.Cond provides:
// Suspends the calling goroutine until the condition is met func (c *Cond) Wait() {} // Wakes up one waiting goroutine, if there is one func (c *Cond) Signal() {} // Wakes up all waiting goroutines func (c *Cond) Broadcast() {}
Alright, let's check out a quick pseudo-example. This time, we've got a Pokémon theme going on, imagine we're waiting for a specific Pokémon, and we want to notify other goroutines when it shows up.
// wait until condition is true for !condition { } // or for !condition { time.Sleep(100 * time.Millisecond) }
In this example, one goroutine is waiting for Pikachu to show up, while another one (the producer) randomly selects a Pokémon from the list and signals the consumer when a new one appears.
When the producer sends the signal, the consumer wakes up and checks if the right Pokémon has appeared. If it has, we catch the Pokémon, if not, the consumer goes back to sleep and waits for the next one.
The problem is, there's a gap between the producer sending the signal and the consumer actually waking up. In the meantime, the Pokémon could change, because the consumer goroutine might wake up later than 1ms (rarely) or other goroutine modifies the shared pokemon. So sync.Cond is basically saying: 'Hey, something changed! Wake up and check it out, but if you're too late, it might change again.'
If the consumer wakes up late, the Pokémon might run away, and the goroutine will go back to sleep.
"Huh, I could use a channel to send the pokemon name or signal to the other goroutine"
Absolutely. In fact, channels are generally preferred over sync.Cond in Go because they're simpler, more idiomatic, and familiar to most developers.
In the case above, you could easily send the Pokémon name through a channel, or just use an empty struct{} to signal without sending any data. But our issue isn't just about passing messages through channels, it's about dealing with a shared state.
Our example is pretty simple, but if multiple goroutines are accessing the shared pokemon variable, let's look at what happens if we use a channel:
That said, when multiple goroutines are modifying shared data, a mutex is still necessary to protect it. You'll often see a combination of channels and mutexes in these cases to ensure proper synchronization and data safety.
"Okay, but what about broadcasting signals?"
Good question! You can indeed mimic a broadcast signal to all waiting goroutines using a channel by simply closing it (close(ch)). When you close a channel, all goroutines receiving from that channel get notified. But keep in mind, a closed channel can't be reused, once it's closed, it stays closed.
By the way, there's actually been talk about removing sync.Cond in Go 2: proposal: sync: remove the Cond type.
"So, what's sync.Cond good for, then?"
Well, there are certain scenarios where sync.Cond can be more appropriate than channels.
"Why is the Lock embedded in sync.Cond?"
In theory, a condition variable like sync.Cond doesn't have to be tied to a lock for its signaling to work.
You could have the users manage their own locks outside of the condition variable, which might sound like it gives more flexibility. It's not really a technical limitation but more about human error.
Managing it manually can easily lead to mistakes because the pattern isn't really intuitive, you have to unlock the mutex before calling Wait(), then lock it again when the goroutine wakes up. This process can feel awkward and is pretty prone to errors, like forgetting to lock or unlock at the right time.
But why does the pattern seem a little off?
Typically, goroutines that call cond.Wait() need to check some shared state in a loop, like this:
// wait until condition is true for !condition { } // or for !condition { time.Sleep(100 * time.Millisecond) }
The lock embedded in sync.Cond helps handle the lock/unlock process for us, making the code cleaner and less error-prone, we will discuss the pattern in detail soon.
If you look closely at the previous example, you'll notice a consistent pattern in consumer: we always lock the mutex before waiting (.Wait()) on the condition, and we unlock it after the condition is met.
Plus, we wrap the waiting condition inside a loop, here's a refresher:
// Suspends the calling goroutine until the condition is met func (c *Cond) Wait() {} // Wakes up one waiting goroutine, if there is one func (c *Cond) Signal() {} // Wakes up all waiting goroutines func (c *Cond) Broadcast() {}
When we call Wait() on a sync.Cond, we're telling the current goroutine to hang tight until some condition is met.
Here's what's happening behind the scenes:
Here's a look at how Wait() works under the hood:
// wait until condition is true for !condition { } // or for !condition { time.Sleep(100 * time.Millisecond) }
Even though it's simple, we can take away 4 main points:
Because of this lock/unlock behavior, there's a typical pattern you'll follow when using sync.Cond.Wait() to avoid common mistakes:
// Suspends the calling goroutine until the condition is met func (c *Cond) Wait() {} // Wakes up one waiting goroutine, if there is one func (c *Cond) Signal() {} // Wakes up all waiting goroutines func (c *Cond) Broadcast() {}
"Why not just use c.Wait() directly without a loop?"
This is an excerpt of the post; the full post is available here: https://victoriametrics.com/blog/go-sync-cond/
The above is the detailed content of Go sync.Cond, the Most Overlooked Sync Mechanism. For more information, please follow other related articles on the PHP Chinese website!