Queue and mailbox

In reply to dave_59:

A mailbox is a built-in class around a queue that uses semaphores to control access to the ends of a queue. A mailbox only has FIFO element ordering whereas you can access the head, tail, or middle elements of a queue.
You typically use a mailbox when there are multiple threads reading and writing data and you need the atomic test-and-set operation of semaphore to know when the mailbox is full or empty.
In the UVM, we use a TLM FIFO which is another wrapper around a mailbox. See How TLM Works | Advanced UVM

Hi Dave,

Recently I faced a racing issue with calling a callback multiple times, which use a shared variable. I remember this topic and see that the mailbox uses a semaphore to control the accessing to it shared queue among threads. So my issue could be the reason why a semaphore is used in the mailbox. Nonetheless, from this answer, and I remember you have already answered somewhere else that SystemVerilog is a single threaded program. Something similar to this. So from my understanding, the race issue for a shared variable cannot happen with functions.

Could you please help me to dig deeper to this concern? I have no idea if we can met race conditions with shared variable among functions? If yes, then how? If no, then why do a semaphore is required for the mailbox since all the queue’s accessing methods are functions.

For the callback code, it belongs to a VIP Vendor and is protected, so I cannot share it here.

Thanks you,
-An