# Delay working when queue involved

**URL:** <https://forums.freertos.org/t/delay-working-when-queue-involved/10799>\
**Category:** Kernel\
**Tags:** tracealyzer\
**Created:** [October 1, 2020, 3:45pm UTC](https://forums.freertos.org/t/delay-working-when-queue-involved/10799 "2020-10-01T15:45:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dj4you](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@dj4you](https://forums.freertos.org/u/dj4you)\
**Post date:** [October 1, 2020, 3:45pm UTC](https://forums.freertos.org/t/delay-working-when-queue-involved/10799/1 "2020-10-01T15:45:44Z")

</div>

In one of my tasks I have code like below

```
fn(){
xQueueReceive(que_handle, portMAX_DELAY){
       // process message
}
vTaskDelay(100)
}

```

Assuming tick time is 1ms, in the above case at what interval mcu processes if it receives a message in queue? is it once in every 100ms or something else?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [October 1, 2020, 4:45pm UTC](https://forums.freertos.org/t/delay-working-when-queue-involved/10799/2 "2020-10-01T16:45:11Z")

</div>

The following call will block forever until a message is received:

```auto
xQueueReceive(que_handle, portMAX_DELAY)

```

The call to `vTaskDelay` will move the task out of ready state for the specified amount of ticks. After the number of ticks have passed, the task will be in the ready state - It will run if there is no other higher priority ready task.

Additionally, you should use `pdMS_TO_TICKS` to convert milliseconds to ticks:

```auto
vTaskDelay( pdMS_TO_TICKS( 100 ) );

```

Thanks.

---

<div class="post-metadata">

**Author:** ![dj4you](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@dj4you](https://forums.freertos.org/u/dj4you)\
**Post date:** [October 1, 2020, 5:10pm UTC](https://forums.freertos.org/t/delay-working-when-queue-involved/10799/3 "2020-10-01T17:10:57Z")

</div>

let us assume due to vTaskDelay task entered in to block state and 50ms is elapsed in block state at that time queue message received, in this case what is the behaviour of freeRTOS?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [October 1, 2020, 5:43pm UTC](https://forums.freertos.org/t/delay-working-when-queue-involved/10799/4 "2020-10-01T17:43:02Z")

</div>

In this specific case, the task will remain blocked for next 50ms. The reason is that by calling `vTaskDelay` you are asking the scheduler to put the task to sleep for 100ms.

If you want to process the received message ASAP, remove `vTaskDelay` and use `xQueueReceive` to block. Just to make it clear, when a task is blocked on a queue to receive a message, it does not consume any CPU cycle.

Thanks.
