FreeExam
A closed notebook, a blank card, and a pencil on a wooden desk.

Concurrency and synchronization

20 cards

Reason about shared-state invariants, memory visibility, progress guarantees, and task lifetime rather than memorizing primitive names.

Card 1 of 20

Front. Concurrent Parallel. Can two tasks be concurrent without executing CPU instructions at the same instant?

These cards are original study material. They are not questions from a named employer or a live hiring loop.

All 20 cards in this deck

  1. 1.Concurrent Parallel

    Can two tasks be concurrent without executing CPU instructions at the same instant?

    Yes. Their lifetimes can overlap while execution is interleaved on one thread. Parallelism requires simultaneous execution; concurrency bugs can exist without it.

  2. 2.Interleaving

    Why can x = x + 1 lose an increment when two threads share x?

    The read, addition, and write can interleave. Protect the whole operation or use an appropriate atomic read-modify-write primitive.

  3. 3.Invariant Lock

    How should the scope of a mutex be chosen for a transfer between two in-memory balances?

    Cover all reads and writes needed to preserve the joint invariant. Locking each assignment separately can expose or create an inconsistent intermediate state.

  4. 4.Visibility

    Why is a shared ready flag insufficient unless its access follows the language's synchronization rules?

    Another thread may not observe the preceding data writes as intended. Use a documented happens-before relationship rather than assuming wall-clock order establishes visibility.

  5. 5.Atomic Composition

    Why does using a thread-safe queue not make checking empty and then removing an item atomic?

    Another consumer can remove the item between calls. Use one queue operation with defined empty or blocking semantics.

  6. 6.Linearization

    What is a linearization point in a concurrent operation?

    A conceptual instant between invocation and completion at which the operation takes effect. It helps show that overlapping calls behave like a legal sequential ordering respecting real-time precedence.

  7. 7.Deadlock Cycle

    Two threads hold one lock each and wait for the other's lock. What structural change breaks this pattern?

    Acquire multiple locks in a shared total order, or avoid holding one while waiting for another. Timeouts can aid recovery but do not establish a correct ordering discipline.

  8. 8.Livelock

    How can two threads keep running without either making progress?

    They may repeatedly react to each other, such as both releasing and retrying in synchrony. Add an asymmetry, bounded randomized backoff, or a coordination rule.

  9. 9.Starvation

    How is starvation different from deadlock?

    Some participants continue making progress while one is repeatedly denied resources or scheduling. Fairness policies address a different property from deadlock freedom.

  10. 10.Condition

    Why must a waiting thread recheck its condition after waking?

    A notification does not reserve the resource, and wakeups can be spurious. Recheck the predicate while holding its associated lock.

  11. 11.Semaphore

    What distinction separates a semaphore from a mutex in a connection limiter?

    A semaphore represents a count of available permits, allowing several concurrent users. A mutex protects exclusive access to a shared invariant and commonly has ownership rules.

  12. 12.Finally Release

    What error can occur if cleanup releases a permit even when acquisition was cancelled?

    The permit count increases without a matching acquisition, violating the concurrency bound. Track successful acquisition and release exactly once.

  13. 13.Cas Retry

    Why must a compare-and-swap loop recompute from the newly observed value after a failed attempt?

    Another operation changed the state. Reusing a result based on the old value can violate the intended update even if a later comparison succeeds.

  14. 14.Aba Generation

    How can versioned state detect an ABA change that value-only comparison misses?

    The value returns to A but its generation advances. Compare both value and generation; also consider wraparound and memory-reclamation requirements.

  15. 15.Lockfree Waitfree

    What distinguishes lock-free progress from wait-free progress?

    Lock-free guarantees that some operation completes under its assumptions. Wait-free bounds each participant's operation steps; lock-free alone permits individual starvation.

  16. 16.Ownership Transfer

    How can transferring exclusive ownership through a channel reduce synchronization complexity?

    Only the receiver mutates the object after transfer. The guarantee fails if the sender keeps and uses a mutable alias, so ownership discipline matters as much as the channel.

  17. 17.Immutable Share

    Why are immutable values easier to share across tasks?

    Readers do not race with mutation after safe publication. Building or publishing the value still needs the language's appropriate visibility guarantees.

  18. 18.Cancel Resource

    A task is cancelled while holding a file handle and a lock. What must cancellation preserve?

    Cleanup and release must occur without leaving partially updated shared state. Cancellation is a control path that needs the same invariant reasoning as exceptions.

  19. 19.Parent Child

    Why should a parent task know when its children finish or fail?

    Otherwise work can outlive its resources, errors can go unobserved, and shutdown can race with ongoing mutations. Use explicit lifetime ownership and intentional background execution.

  20. 20.Proof Test

    Why should concurrency tests assert invariants in addition to running under a race detector?

    Race detectors find exercised unsynchronized accesses; they do not prove ordering, atomic business behavior, fairness, or all possible schedules. Combine controlled schedules with invariant checks.