#  same Queue Receive in two different tasks

**URL:** <https://forums.freertos.org/t/same-queue-receive-in-two-different-tasks/3482>\
**Category:** Kernel\
**Created:** [September 24, 2015, 9:30am UTC](https://forums.freertos.org/t/same-queue-receive-in-two-different-tasks/3482 "2015-09-24T09:30:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 24, 2015, 9:30am UTC](https://forums.freertos.org/t/same-queue-receive-in-two-different-tasks/3482/1 "2015-09-24T09:30:47Z")

</div>

**[joe\_her](https://sourceforge.net/u/joe_her/)** wrote on [Thursday, September 24, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/97e2d943/#5437):

Can I wait for the same queue msg in two different tasks? That is one task sends a msg, which shuold be recieved by two or more then one task. How can this be done? the main concern is who will clear the queue, and how can one make sure that the queue is cleard only after all “clients” got the messege?  
I am a bit confused on this.  
Thnaks

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [September 24, 2015, 9:37am UTC](https://forums.freertos.org/t/same-queue-receive-in-two-different-tasks/3482/2 "2015-09-24T09:37:28Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, September 24, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/97e2d943/#3f16):

Maybe each task could use [xQueuePeek()](http://www.freertos.org/xQueuePeek.html) to read the message from the queue without removing the message from the queue, and then [set a bit in an event group](http://www.freertos.org/xEventGroupSetBits.html) to indicate that that task had obtained the message. When the value returned from xEventGroupSetBits() indicates that all the tasks had read the message then the message can be removed from the queue.

Just one suggestion - there are probably other methods that could be used too.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 24, 2015, 9:41am UTC](https://forums.freertos.org/t/same-queue-receive-in-two-different-tasks/3482/3 "2015-09-24T09:41:57Z")

</div>

**[joe\_her](https://sourceforge.net/u/joe_her/)** wrote on [Thursday, September 24, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/97e2d943/#d143):

Thanks, but isnt there a racing risk? it looks like taskENTER\_CRITICAL should be used between Peek and setting the bit. So I wonder if there is a better, more elegant, way.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [September 24, 2015, 9:51am UTC](https://forums.freertos.org/t/same-queue-receive-in-two-different-tasks/3482/4 "2015-09-24T09:51:41Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, September 24, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/97e2d943/#d143/a95d):

You should not call the peek function from a critical section, at least,  
not if a block time is specified.

I would need to know more about the use case to know if a race condition  
existed, as it would depend on what the tasks that had read from the  
queue did next. If they were to immediately try reading again then they  
too would have to check the event group to ensure all the tasks had read  
the message before they tried reading again.

It also depends on the characteristics of how the queue is used. If  
there is only one item available then you might implement a scheme where  
by tasks subscribe to the message, then when a message arrives on the  
queue it is sent to the tasks individually, maybe even using a task  
notification.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 24, 2015, 10:29am UTC](https://forums.freertos.org/t/same-queue-receive-in-two-different-tasks/3482/5 "2015-09-24T10:29:21Z")

</div>

**[joe\_her](https://sourceforge.net/u/joe_her/)** wrote on [Thursday, September 24, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/97e2d943/#3340):

Thnaks.  
Will try that. As each event bit will be set by a different task, I think there is no risk of a race condition, assuming that setting the event bit is atomic, and this read-modify-write can not be interrupted by another task.  
Johanan

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [September 24, 2015, 12:01pm UTC](https://forums.freertos.org/t/same-queue-receive-in-two-different-tasks/3482/6 "2015-09-24T12:01:31Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Thursday, September 24, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/97e2d943/#7bab):

My general feeling is that if it is only two tasks, and they don’t need to keep ‘in sync’ while processing messages from the queue, that it would be much simpler to just create two queues and have the sender place the message on both.

If it is something more complicated, then maybe a better description would help.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 24, 2015, 12:04pm UTC](https://forums.freertos.org/t/same-queue-receive-in-two-different-tasks/3482/7 "2015-09-24T12:04:29Z")

</div>

**[joe\_her](https://sourceforge.net/u/joe_her/)** wrote on [Thursday, September 24, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/97e2d943/#9892):

Aha… that’s the simple solution.  
Why didn’t I think of that??? 🙂

Thanks
