Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • How to Use This Puzzle Bank
  • SystemVerilog Language Puzzles25
  • Difficulty Guide
  • Full Topic Map
  • Interview-Day Tips
  • OOP & Testbench Puzzles25
  • Concurrency & Timing Puzzles25
    • Event Scheduling Puzzles
    • Race Condition Puzzles
      • Competing Active Writers on x
      • Blocking Writer vs NBA Writer Same Signal
      • False Mismatch from Active Sampling
      • #0 Monitor Is Still Before NBA
      • Lost Update in Forked Counter
      • Two always_ff Writers, One Register
    • Clocking Block Puzzles
    • Semaphore & Mailbox Puzzles
    • Concurrency & Timing Puzzles: Tricky Q&A
  • Randomization & Constraint Puzzles25
  • SVA Assertion Puzzles25
  • Functional Coverage Puzzles25
  • Logic & Aptitude Puzzles25
  • Scenario & Debug Puzzles25
  • Interview Prep: Puzzle Rounds5
  • DV Puzzle Cheatsheet
Loading lesson

‹Race Condition Puzzles
  1. Topics
  2. DV Interview Puzzles
  3. Concurrency & Timing Puzzles
  4. Race Condition Puzzles
  5. Lost Update in Forked Counter

VLSI DV Interview Puzzles · All levels

Lost Update in Forked Counter

Compute final hits and explain why four increments do not always produce four.

On this page

    Puzzle

    Difficulty: Hard · Puzzle 5 of 6 · Topic: Race Condition Puzzles

    Compute final hits and explain why four increments do not always produce four.

    Code

    tb.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    module tb;
      int hits = 0;
    
      task automatic add_hit();
        int snap;
        snap = hits;
        #0 hits = snap + 1;
      endtask
    
      initial begin
        fork
          repeat (2) add_hit();
          repeat (2) add_hit();
        join
        $display("[%0t] hits=%0d", $time, hits);
      end
    endmodule

    Hint

    Read-modify-write is not atomic across concurrent threads.

    Step-by-step solution

    Diagram
    1) Multiple threads read same hits snapshot before any #0 write occurs.
    2) Writes happen later in inactive and overwrite each other.
    3) Typical final result is 2 instead of 4 because two updates are lost.
    4) Correct fix needs synchronization (semaphore/atomic handoff), not timing hacks.

    Answer

    Answer: Final hits is typically 2, not 4, due to concurrent stale snapshots and overwrite races.

    Why candidates get it wrong

    Candidates assume integer assignment is 'thread-safe' in simulation. It is not.

    Interviewer follow-up

    Show a semaphore-based rewrite that guarantees hits=4.

    Related topics

    • Back to Race Condition Puzzles

    • Concurrency & Timing Puzzles section

    Previous

    65 / 210

    Next: Two always_ff Writers, One Register

    Up next

    Up nextTwo always_ff Writers, One Register

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Competing Active Writers on x
    • Blocking Writer vs NBA Writer Same Signal
    • False Mismatch from Active Sampling