# Queue and mailbox

**URL:** <https://verificationacademy.com/forums/t/queue-and-mailbox/28832>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog\
**Created:** [September 24, 2013, 1:53am UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832 "2013-09-24T01:53:56Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![imran](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/i/7feea3/32.png) [@imran](https://verificationacademy.com/forums/u/imran)\
**Post date:** [September 24, 2013, 1:53am UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/1 "2013-09-24T01:53:56Z")

</div>

Hi  
1.I want to know what is the main difference between queues and mailbox,what are the advantages of them.  
2.What is the advantage of mailbox over FIFO.

Thanks and regards,  
Imran

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [September 24, 2013, 4:13am UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/2 "2013-09-24T04:13:21Z")

</div>

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 | UVM/OVM Verification Methodology | Verification Academy](https://verificationacademy.com/sessions/how-tlm-works)

---

<div class="post-metadata">

**Author:** ![imran](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/i/7feea3/32.png) [@imran](https://verificationacademy.com/forums/u/imran)\
**Post date:** [September 24, 2013, 6:49am UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/3 "2013-09-24T06:49:32Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/2):_

Thanks dave.

---

<div class="post-metadata">

**Author:** ![myselfprakhar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@myselfprakhar](https://verificationacademy.com/forums/u/myselfprakhar)\
**Post date:** [May 15, 2014, 9:40am UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/4 "2014-05-15T09:40:58Z")

</div>

Hi,

I am not getting the advantage of mailbox over queues.  
As what I got is that in both we can put and get the object handles or values. In queue we can get any value from the position we want. while in mailbox in build methods are there put,get,try\_put, try\_get etc. so how is mailbox is more efficient than queues?

Thanks

---

<div class="post-metadata">

**Author:** ![Subhash1](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/subhash1/32/2158_2.png) [@Subhash1](https://verificationacademy.com/forums/u/Subhash1)\
**Post date:** [May 15, 2014, 10:47am UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/5 "2014-05-15T10:47:02Z")

</div>

_In reply to [myselfprakhar](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/4):_

You can say Mailbox has multiple insertion points

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [May 15, 2014, 11:04am UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/6 "2014-05-15T11:04:33Z")

</div>

_In reply to [Subhash](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/5):_

A queue is just a data structure, and a mailbox is an higher level concept that is built around a combination of queues and semaphores. If you have only one process reading and writing to the data structure, there is no need to use a mailbox. However if there are more than one thread, a mailbox is a convenient class to use. But if you have multiple threads, and need access to any position in the queue, you will need to write your own class to do that. The mailbox concept is in many other programming languages, so you should be able to search for more information about it.

---

<div class="post-metadata">

**Author:** ![myselfprakhar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@myselfprakhar](https://verificationacademy.com/forums/u/myselfprakhar)\
**Post date:** [May 15, 2014, 11:46am UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/7 "2014-05-15T11:46:06Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/6):_

Thanks

---

<div class="post-metadata">

**Author:** ![Ankit\_Patel](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/ankit_patel/32/1302_2.png) [@Ankit\_Patel](https://verificationacademy.com/forums/u/Ankit_Patel)\
**Post date:** [September 22, 2014, 5:19pm UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/8 "2014-09-22T17:19:04Z")

</div>

_In reply to [myselfprakhar](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/7):_

Mailbox is similar to a queue, which allows only atomic operations. They can be bounded/unbounded. Bounded means it can be configured to fix size. Mailbox size can be configured to fix or unbounded while queue size is unbounded. Get/put task is used to suspend a bounded mailbox. That’s why mailbox is used more for communication between threads. FIFO can be implemented using mailbox. While FIFO or LIFO can be implemented using queue.

---

<div class="post-metadata">

**Author:** ![rgarcia07](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a87d85/32.png) [@rgarcia07](https://verificationacademy.com/forums/u/rgarcia07)\
**Post date:** [April 27, 2018, 7:20pm UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/9 "2018-04-27T19:20:13Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/8):_

Hi Dave,

Apologies for using this thread to ask a unrelated question, you mentioned about TLM Fifo in UVM as a wrapper of a SV mailbox, I was wondering depending on the application if this can produce a big performance penalty, let’s say I’m building a reference model for a ‘complex router’ (I know early optimisations are the cause of big problems most of times), I also do understand the intent of using UVM (reusability, consistency, etc) but where to draw the line of using the methodology or not?

Cheers,

-R

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [April 27, 2018, 9:14pm UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/10 "2018-04-27T21:14:33Z")

</div>

_In reply to [rgarcia07](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/9):_

The design of the UVM is based on the old-school premise that the DUT overwhelms the testbench simulation. IMHO, not enough effort is put into analyzing and optimizing the performance of the UVM Base Class Library.

If you think your testbench going to have a performance penalty, then most likely it will. But it’s now always in the place you expect it to be. You’ll need to do some careful experiments and profiling to find out.

---

<div class="post-metadata">

**Author:** ![rgarcia07](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/a87d85/32.png) [@rgarcia07](https://verificationacademy.com/forums/u/rgarcia07)\
**Post date:** [April 27, 2018, 9:50pm UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/11 "2018-04-27T21:50:01Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/10):_

> _In reply to [rgarcia07](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/10):_  
> The design of the UVM is based on the old-school premise that the DUT overwhelms the testbench simulation. IMHO, not enough effort is put into analyzing and optimizing the performance of the UVM Base Class Library.  
> If you think your testbench going to have a performance penalty, then most likely it will. But it’s now always in the place you expect it to be. You’ll need to do some careful experiments and profiling to find out.

Thank you very much for your inputs Dave :-)

---

<div class="post-metadata">

**Author:** ![AnPham](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@AnPham](https://verificationacademy.com/forums/u/AnPham)\
**Post date:** [July 4, 2019, 5:15am UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/12 "2019-07-04T05:15:11Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/11):_

> 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](https://verificationacademy.com/sessions/how-tlm-works)

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](https://stackoverflow.com/questions/26449803/uvm-shared-variables/26453953#26453953), and I remember you have already answered somewhere else that SystemVerilog is a single threaded program. Something similar to [this](https://verificationacademy.com/forums/uvm/can-system-verilog-c-code-work-parallel-while-using-dpi#answer-40265). 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

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [July 4, 2019, 1:14pm UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/13 "2019-07-04T13:14:34Z")

</div>

_In reply to [AnPham](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/12):_

As an HDL, Verilog was designed as if code could be run in parallel, even though it was implemented in a single threaded program. This allows much more flexibility in optimizations of the code, as well as eventual parallel implementation.

But a semaphore also provides FIFO thread access to shared variables. It also has queues to make sure each thread waiting for access get its access in order.

---

<div class="post-metadata">

**Author:** ![AnPham](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@AnPham](https://verificationacademy.com/forums/u/AnPham)\
**Post date:** [July 5, 2019, 3:37am UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/14 "2019-07-05T03:37:02Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/13):_

> _In reply to [AnPham](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/13):_  
> As an HDL, Verilog was designed as if code could be run in parallel, even though it was implemented in a single threaded program. This allows much more flexibility in optimizations of the code, as well as eventual parallel implementation.  
> But a semaphore also provides FIFO thread access to shared variables. It also has queues to make sure each thread waiting for access get its access in order.

Hi Dave, thank you for you reply. It still confuses me, but I will do further researches later.  
-An

---

<div class="post-metadata">

**Author:** ![Ankit\_jha](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@Ankit\_jha](https://verificationacademy.com/forums/u/Ankit_jha)\
**Post date:** [July 30, 2025, 7:39am UTC](https://verificationacademy.com/forums/t/queue-and-mailbox/28832/15 "2025-07-30T07:39:34Z")

</div>

Hi ,

Although a System Verilog mailbox essentially behaves like a _queue_ , it is quite different from the `queue` data type. A simple queue can only push and pop items from either the front or the back. However, a _mailbox_ is a built-in class that uses [semaphores]semaphore) to have atomic control the push and pop from the queue. Moreover, you cannot access a given index within the mailbox queue, but only retrieve items in FIFO order.
