Back to all reviewers

Thread-safe state transitions

oven-sh/bun
Based on 2 comments
C++

When modifying shared state in multithreaded code, implement proper checks and state transitions to prevent race conditions. This applies to flags, counters, and resource management.

Concurrency C++

Reviewer Prompt

When modifying shared state in multithreaded code, implement proper checks and state transitions to prevent race conditions. This applies to flags, counters, and resource management.

  1. Use atomic operations when checking and modifying shared state
  2. Check state values after atomic operations rather than before
  3. For multi-step processes, track in-flight operations and only perform final state transitions when all operations are complete

Bad:

// Don't check flags without thread safety
if (m_terminationFlags & TerminatedFlag) {
    // Already terminated, skip work
    return;
}
// Race condition: Another thread might have set the flag between check and action

Good:

// Use atomic operations and check the return value
if (atomic_fetch_or(&m_terminationFlags, TerminatedFlag) & TerminatedFlag) {
    // Already terminated, skip work
    return;
}

// For multi-step processes, track completion:
protectedThis->m_messagesInFlight -= 1;
if (protectedThis->m_messagesInFlight == 0)
    protectedThis->close(); // Only close when all messages are processed
2
Comments Analyzed
C++
Primary Language
Concurrency
Category

Source Discussions