# Semaphore

**URL:** <https://verificationacademy.com/forums/t/semaphore/39253>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog\
**Created:** [July 1, 2021, 12:53am UTC](https://verificationacademy.com/forums/t/semaphore/39253 "2021-07-01T00:53:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![timag](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/t/9de053/32.png) [@timag](https://verificationacademy.com/forums/u/timag)\
**Post date:** [July 1, 2021, 12:53am UTC](https://verificationacademy.com/forums/t/semaphore/39253/1 "2021-07-01T00:53:05Z")

</div>

Hi  
Why semaphore only can be used in task instead of function?  
if i want using in function, how should i do? i have two functions running parallel to access a common resource  
Thanks

---

<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 1, 2021, 1:33am UTC](https://verificationacademy.com/forums/t/semaphore/39253/2 "2021-07-01T01:33:14Z")

</div>

_In reply to [timag](https://verificationacademy.com/forums/t/semaphore/39253):_

Because tasks can potentially consume time (block/suspend), whereas functions cannot. The get() method of a semaphore is a task that can potentially block waiting for a key.

You could use the function try\_get() instead. However, you cannot really have two functions running in parallel because they both consume 0 time. One will execute before the other.

---

<div class="post-metadata">

**Author:** ![timag](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/t/9de053/32.png) [@timag](https://verificationacademy.com/forums/u/timag)\
**Post date:** [July 1, 2021, 1:59am UTC](https://verificationacademy.com/forums/t/semaphore/39253/3 "2021-07-01T01:59:16Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/semaphore/39253/2):_  
there is try\_get() API in semaphore. do u mean that i use it?

---

<div class="post-metadata">

**Author:** ![bdreku](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bdreku/32/4520_2.png) [@bdreku](https://verificationacademy.com/forums/u/bdreku)\
**Post date:** [July 1, 2021, 4:03am UTC](https://verificationacademy.com/forums/t/semaphore/39253/4 "2021-07-01T04:03:44Z")

</div>

_In reply to [timag](https://verificationacademy.com/forums/t/semaphore/39253/3):_  
Either you can use try\_get() or you can use fork-join\_none in function which gets the key and also you can have the logic after getting a key.

---

<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 1, 2021, 6:36am UTC](https://verificationacademy.com/forums/t/semaphore/39253/5 "2021-07-01T06:36:32Z")

</div>

_In reply to [timag](https://verificationacademy.com/forums/t/semaphore/39253/4):_

It really doesn’t make much sense to use semaphores in functions. Yes you can call try\_get() from another function. But what if it returns false, then what do you do? You can’t keep calling it until it returns true because you get into a infinite zero-delay loop.

---

<div class="post-metadata">

**Author:** ![timag](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/t/9de053/32.png) [@timag](https://verificationacademy.com/forums/u/timag)\
**Post date:** [July 2, 2021, 2:09am UTC](https://verificationacademy.com/forums/t/semaphore/39253/6 "2021-07-02T02:09:59Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/semaphore/39253/5):_

for example , i have queue in my code. And I have two class A and B.  
there is functionA in classA which put some data to the queue. Another function B is to clean  
the queue data. Two functions running paraelly, I worry that when function A is putting some data and functionA is cleaning queue at the same time. therefore i want to ensure function B should clear the queue only for no one accessing this queue.

---

<div class="post-metadata">

**Author:** ![Rsignori92](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/3da27b/32.png) [@Rsignori92](https://verificationacademy.com/forums/u/Rsignori92)\
**Post date:** [July 2, 2021, 8:23am UTC](https://verificationacademy.com/forums/t/semaphore/39253/7 "2021-07-02T08:23:13Z")

</div>

_In reply to [timag](https://verificationacademy.com/forums/t/semaphore/39253/6):_

Hello all, Why not simply declaring those pop and push function as tasks? Having a function that does it for you could cause issues due to the 0 delay execution which is dangerous in case of loops. Is there amy restrictions in using function instead of task ?.

Trying to call try\_get and try\_put i believe could cause some clashes since a 0 delay function could be executed before another in a not known manner i believe. Regards

---

<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 3, 2021, 4:21am UTC](https://verificationacademy.com/forums/t/semaphore/39253/8 "2021-07-03T04:21:48Z")

</div>

_In reply to [timag](https://verificationacademy.com/forums/t/semaphore/39253/7):_

> _In reply to [dave\_59](https://verificationacademy.com/forums/t/semaphore/39253/7):_  
> for example , i have queue in my code. And I have two class A and B.  
> there is functionA in classA which put some data to the queue. Another function B is to clean  
> the queue data. Two functions running paraelly, I worry that when function A is putting some data and functionA is cleaning queue at the same time. therefore i want to ensure function B should clear the queue only for no one accessing this queue.

What you’re describing still makes no sense. A and B are functions that do not consume time. The put and the clear are in a race to modify the queue, last modification wins.

But even if you could use a semaphore, the get needs to be able to suspend if the resource is not available. You can’t do that in a function. And if you use try\_get() and it fails, you still need to be able to suspend for some amount before you try to call try\_get again.
