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
    • Clocking Block Puzzles
    • Semaphore & Mailbox Puzzles
      • Lock Held Across Blocking Response Wait
      • Bounded Mailbox put Deadlock
      • try_get Busy Spin at Time 0
      • Leaked Semaphore Token on Error Path
      • Two Consumers Waiting on One Mailbox
      • Deadlock-Free Request Loop with try_put/try_get
    • 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

‹Semaphore & Mailbox Puzzles
  1. Topics
  2. DV Interview Puzzles
  3. Concurrency & Timing Puzzles
  4. Semaphore & Mailbox Puzzles
  5. Deadlock-Free Request Loop with try_put/try_get

VLSI DV Interview Puzzles · All levels

Deadlock-Free Request Loop with try_put/try_get

Does this pattern avoid deadlock? Explain why it still needs timing control and bounded retries.

On this page

    Puzzle

    Difficulty: Medium · Puzzle 6 of 6 · Topic: Semaphore & Mailbox Puzzles

    Does this pattern avoid deadlock? Explain why it still needs timing control and bounded retries.

    Code

    tb.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    module tb;
      mailbox #(int) req_mb = new(2);
      mailbox #(int) rsp_mb = new(2);
      semaphore lock = new(1);
    
      task producer();
        int rsp;
        int sent = 0;
        while (!sent) begin
          if (req_mb.try_put(7)) sent = 1;
          else #1;
        end
        while (!rsp_mb.try_get(rsp)) #1;
        $display("[%0t] rsp=%0d", $time, rsp);
      endtask
    
      task consumer();
        int req;
        while (!req_mb.try_get(req)) #1;
        lock.get(1);
        rsp_mb.put(req + 1);
        lock.put(1);
      endtask
    
      initial fork
        producer();
        consumer();
      join
    endmodule

    Hint

    No thread holds lock while waiting on mailbox progress.

    Step-by-step solution

    Diagram
    1) Producer retries try_put/try_get with #1 backoff, so no zero-delay spin.
    2) Consumer acquires lock only around non-blocking short critical section.
    3) No circular wait exists between lock and mailbox operations.
    4) Pattern is deadlock-resistant but still needs timeout/limits for robustness.

    Answer

    Answer: Yes, this avoids classic lock+blocking-mailbox deadlock. It should print rsp=8 at a finite time, but production code still needs timeout guards.

    Why candidates get it wrong

    Deadlock-free does not mean livelock-free or starvation-free under all load patterns.

    Interviewer follow-up

    What timeout and error-report strategy would you add for regression triage?

    Related topics

    • Back to Semaphore & Mailbox Puzzles

    • Concurrency & Timing Puzzles section

    Previous

    78 / 210

    Next: Concurrency & Timing Puzzles: Tricky Q&A

    Up next

    Up nextConcurrency & Timing Puzzles: Tricky Q&A

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • Lock Held Across Blocking Response Wait
    • Bounded Mailbox put Deadlock
    • try_get Busy Spin at Time 0