Mailbox query

In this eg.

module automatic bounded; 
 mailbox mbx;  
 initial begin   
  mbx  = new(1);  // Size  = 1   
  fork       // Producer  
     for (int i=1; i<4; i++) begin  
      $display("@%0d: Producer: putting %0d",  $time,  i);  
      mbx.try_peek(i); 
      $display("@%0d: Producer: put(%0d) done  %0d",            $time, i);   
     end       // Consumer      
 repeat(3) begin  
 int j; 
 #1ns mbx.try_get(j); 
 $display("@%0d: Consumer: got %0d", $time, j);   
    end    
 join 
 end 
endmodule

Here according to the output
Three times try_peek executes and writes 1,2,3
And then try_get executes 3 times and it reads 0,0,0

I am not understanding why is it reading 0 all the 3 times?

In reply to Shipra_s:

A couple of problems with your code, the major one using try_peek() instead of try_put().

All of the mailbox try_put/peek/get() methods are functions that that return a success status and you choose a simulator that does not warn you about it. You need to test result for success (non-zero). Had you done that you would have seen that try_peek() returns zero, and maybe figured it out.

Your second $display statement has three %d format specifiers, but only two arguments.

I do not recommend the use of program blocks.

I do recommend parameterizing your mailbox with the type of the item being put into it.

mailbox #(int) mbx;

You will appreciate it better when you have many mailboxes and make a mistake in using the wrong mailbox. It gives you an immediate compilation error.