Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Semaphore: A Semaphore With Multiple Keys

SystemVerilog Interprocess Communication

Runnable example from the SystemVerilog Semaphore tutorial (A Semaphore With Multiple Keys)

Open in IDE
testbench.sv
module tb;
  semaphore pool;

  task automatic use_resource (int id);
    pool.get (1);
    $display ("[%0t] Worker %0d acquired a resource", $time, id);
    #10 pool.put (1);
    $display ("[%0t] Worker %0d released a resource", $time, id);
  endtask

  initial begin
    pool = new (3);   // 3 resources available at once

    fork
      use_resource (1);
      use_resource (2);
      use_resource (3);
      use_resource (4);   // no key left for this one - has to wait
    join
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.