bigonotes
CS ConceptsLocks and Syncing

sleep() vs wait()

Why sleep() keeps your locks while wait() releases them, and where yield() fits in.

3 min readLast updated

sleep() and wait() both move a thread into the Waiting state, but they mean very different things. sleep() is a timed pause that keeps any locks the thread holds. wait() is for coordination: it releases the lock and waits until another thread signals. Mixing them up is a common source of stalls in concurrent code.

In Python, these are time.sleep() and threading.Condition.wait(). Here's the whole difference in two lines:

sleep() keeps the lock, waits for a timer, then goes back to the ready queue on its own.

wait() releases the lock and waits until another thread calls notify() / notify_all() (or an optional timeout expires).

sleep(): "wake me after N seconds"#

Use sleep() when a thread has nothing to do for a while. A sleeping thread gives up the CPU for a fixed time.

import time

time.sleep(5)  # don't need the CPU for 5 seconds
  • The thread moves to Waiting (a timed wait).
  • It does not release any locks it holds.
  • When the timer expires, the OS moves it back to Ready. Nobody has to wake it.

wait(): "I can't continue until someone tells me"#

Use wait() when a thread needs another thread to do something first. A waiting thread hands back the lock and sleeps until another thread signals that the condition may have changed.

import threading

cond = threading.Condition()
data_ready = False

def consumer():
    with cond:
        while not data_ready:
            # Releases cond's lock and sleeps; re-acquires it before returning
            cond.wait()
        # data_ready is True and this thread holds the lock again
        print("got data")

def producer():
    global data_ready
    with cond:
        data_ready = True
        cond.notify_all()  # wake the waiters

t = threading.Thread(target=consumer)
t.start()
producer()
t.join()
  • The thread moves to Waiting.
  • It releases the condition's lock, so other threads can enter and change the condition.
  • It stays waiting until another thread calls notify() or notify_all() on the same condition (or until the timeout, if you call wait(timeout)).
  • You must hold the lock to call wait(), or Python raises RuntimeError.
  • Always check the condition in a while loop (or use cond.wait_for(predicate)): threads can wake spuriously (with no notify() at all), or another thread may have consumed the condition first.

Why the lock difference matters#

Suppose Thread 1 holds lock A and then pauses. What happens to Thread 2, which also needs A, depends on how Thread 1 pauses:

Thread 1 calls sleep(5) Thread 1 calls wait()
Before pausing Holds lock A Holds lock A
Lock A while paused Still held Released
Thread 2 needs A Blocked for 5 s Proceeds now

Calling sleep() while holding a lock is usually a bug: everyone else waits for no reason. wait() exists so the thread that will satisfy the condition can get in.

yield(): a hint, not a wait#

A third call often comes up next to these two. Some runtimes provide a "yield" call that offers the CPU to other threads without waiting for anything. In Python, time.sleep(0) plays this role (on Unix, os.sched_yield() is the direct system call).

import time

time.sleep(0)  # willing to give up the CPU, but not required to

The thread stays Ready (runnable) and only hints that others may go first. The scheduler is free to ignore it. You'll rarely need it.

Comparison#

The three calls differ in state, locks and who wakes the thread.

sleep() wait() yield
State Waiting (timed) Waiting Ready
Releases lock? No Yes N/A
Wakes when Timer expires notify()/notify_all() or timeout Scheduler decides
Purpose Timed pause Coordinating threads Scheduler hint
Typical use Polling delays, simple throttling Producer–consumer, condition waiting Almost never

Key takeaways#

  • sleep() pauses for a fixed time and keeps every lock it holds.
  • wait() releases the lock, waits for a signal, and re-acquires the lock before returning.
  • Always call wait() while holding the condition's lock, in a while loop that re-checks the condition.
  • Sleeping while holding a lock is almost always a bug.
  • Yielding is only a hint; the thread stays runnable.
Revision notes · 1 min

Core idea#

sleep() and wait() both move a thread to Waiting. sleep() is a timed pause that keeps every lock. wait() releases the lock and waits for another thread to signal.

Key points#

  • In Python these are time.sleep() and threading.Condition.wait().
  • A sleeping thread returns to Ready when its timer expires. Nobody has to wake it.
  • A waiting thread releases the condition's lock, so other threads can change the condition.
  • It wakes on notify() or notify_all(), or on the timeout in wait(timeout).
  • wait() re-acquires the lock before it returns.
  • Call wait() while you hold the lock. Otherwise Python raises RuntimeError.
  • Yield is a hint: time.sleep(0) or os.sched_yield(). The thread stays Ready, and the scheduler may ignore it.

Comparison#

sleep() wait() Yield
State Waiting (timed) Waiting Ready
Releases lock? No Yes N/A
Wakes when Timer expires notify()/notify_all() or timeout Scheduler decides
Purpose Timed pause Coordinating threads Scheduler hint
Typical use Polling delays, throttling Producer–consumer, condition waiting Almost never

Pitfalls#

  • Sleeping while you hold a lock blocks every other thread that needs it, for no reason.
  • Check the condition in a while loop, or use cond.wait_for(predicate). Threads wake spuriously, or another thread consumes the condition first.

Say it in an interview#

  • "sleep() keeps the lock. wait() gives it back so the thread that satisfies the condition can get in."
  • "Always call wait() under the lock, inside a while loop."

Found a mistake? Email ask@bigonotes.com