How to clear a Mailbox?

Hi Everyone,

I am using Systemverilog Mailbox in my testbench. I need to clear it’s contents after it’s use is over. I searched online and found the following solution:


mailbox example_mb();
int temp_data;

//After using the mailbox, clear it's content
while (example_mb.try_get(temp_data));

Can’t we assign null to example_mb handle in this case? I can assign back the object, if required.

Regards,
Vishal

In reply to Vishal D:
Can you explain what you mean by “clear its contents after its use is over”? If you are done with it, why do care about its contents?

If you mean you have entries that were put() into the mailbox that you do not plan to get() and want to remove them from the mailbox, then you do plan on continuing to use the mailbox. The while() loop you found will flush the contents of the mailbox and leave it empty.

Assigning null to example_mb removes access to the mailbox. The mailbox gets removed if there are no other handles to that mailbox in existence.