# Question About Critical And Create a New Task

**URL:** <https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962>\
**Category:** Kernel\
**Tags:** debug\
**Created:** [August 9, 2023, 8:24am UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962 "2023-08-09T08:24:09Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![passerby](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@passerby](https://forums.freertos.org/u/passerby)\
**Post date:** [August 9, 2023, 8:24am UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962/1 "2023-08-09T08:24:10Z")

</div>

After I entered the critical , I created a new task, but the critical did not exit. Should my new task run? This new task has the same priority as my current task. Thks.

---

<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:** [August 9, 2023, 11:34am UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962/2 "2023-08-09T11:34:00Z")

</div>

What do you mean by “but the critical did not exit”.

Critical sections only end when you call the taskEXIT\_CRITICAL() function.  
While in a critical section, the scheduler will not switch to a new task (at least if you follow the rules)

Note the line from taskENTER\_CRITICAL:  
FreeRTOS API functions must not be called from within a critical section.

So, creating a task inside one (which requires calling a FreeRTOS API) isn’t a defined operation.

I will also note that creating a task is probably a bigger operation than what I would do under a critical section (if it was allowed), but maybe your interrupt requirements aren’t very strict.

Also,

---

<div class="post-metadata">

**Author:** ![ydhuang](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/ydhuang/32/3763_2.png) [@ydhuang](https://forums.freertos.org/u/ydhuang)\
**Post date:** [August 9, 2023, 4:58pm UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962/3 "2023-08-09T16:58:15Z")

</div>

As @richard-damon pointed out, could you share a code snippet of your code so that it is clear what your problem is?

---

<div class="post-metadata">

**Author:** ![passerby](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@passerby](https://forums.freertos.org/u/passerby)\
**Post date:** [August 10, 2023, 1:28am UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962/4 "2023-08-10T01:28:56Z")

</div>

@richard-damon@ydhuang  
Hello , This is my code , thanks!

```auto
void Task1(void *parameter) 
{
    taskENTER_CRITICAL();
    xTaskCreate(Task2);
    taskEXIT_CRITICAL(); 
    printf("This is Task 1\r\n");
    for(;;)
    {
         vTaskDelay(10);
    }
}

```

```auto
void Task2(void *parameter)
{
    printf("This is Task 2\r\n");
    for(;;)
    {
         vTaskDelay(10);
    }
}

```

The result is that the “This is Task 2” is printed first and the “This is Task 1” is printed later

---

<div class="post-metadata">

**Author:** ![ActoryOu](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/actoryou/32/2880_2.png) [@ActoryOu](https://forums.freertos.org/u/ActoryOu)\
**Post date:** [August 10, 2023, 2:37am UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962/5 "2023-08-10T02:37:40Z")

</div>

Hi @passerby,  
From your code snippet, task1 has exited the critical section before calling `printf`. So that gives scheduler chance to do context switch from task1 to task2.

And you might be interested in the reason of preemption of same priority. Refer to [configUSE\_PREEMPTION](https://www.freertos.org/a00110.html#configUSE_PREEMPTION) and [configUSE\_TIME\_SLICING](https://www.freertos.org/a00110.html#configUSE_TIME_SLICING), scheduler does context switch for same priority task when these two configurations are enabled.

Thanks.

---

<div class="post-metadata">

**Author:** ![passerby](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@passerby](https://forums.freertos.org/u/passerby)\
**Post date:** [August 10, 2023, 2:43am UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962/6 "2023-08-10T02:43:24Z")

</div>

Hi , @ActoryOu :  
If task1 changes as follows

```auto
void Task1(void *parameter) 
{
    taskENTER_CRITICAL();
    xTaskCreate(Task2);
    printf("This is Task 1\r\n");
    taskEXIT_CRITICAL(); 
    for(;;)
    {
         vTaskDelay(10);
    }
}

```

The results are still the same, “This is Task 2” is printed first and the “This is Task 1” is printed later. Is this normal?

Thanks.

---

<div class="post-metadata">

**Author:** ![ActoryOu](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/actoryou/32/2880_2.png) [@ActoryOu](https://forums.freertos.org/u/ActoryOu)\
**Post date:** [August 10, 2023, 3:17am UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962/7 "2023-08-10T03:17:05Z")

</div>

Hi,  
I’ve tried windows simulator and it works as expection.  
Could you help to provide more info of your environment? (platform, FreeRTOS version, FreeRTOSConfig.h)

output from WinSim:

> vTaskStartScheduler  
> This is Task 1  
> This is Task 2

Thanks.

---

<div class="post-metadata">

**Author:** ![passerby](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@passerby](https://forums.freertos.org/u/passerby)\
**Post date:** [August 10, 2023, 3:25am UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962/8 "2023-08-10T03:25:26Z")

</div>

Hi @ActoryOu :

My platform is ZYNQ7000 , Version is V10.0.0 , configUSE\_PREEMPTION 1  
configUSE\_TIME\_SLICING 1.

I use time slice scheduling , the phenomenon I’m looking for is to print Task1 first. So I’m confused. Are there any possible reasons for this error phenomenon

Thanks.

---

<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:** [August 10, 2023, 4:02am UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962/9 "2023-08-10T04:02:20Z")

</div>

As I mentioned at the beginning, your code is invalid, and no behavior promised, as you are calling an API function inside a critical section. This is just not allowed.

Perhaps you should just suspend the scheduler, then create the task. You still might not be able to call prints, as it might block on the I/O and you aren’t allowed to do that with the scheduler suspended.

The big question perhaps is what are you actually trying to do, i.e.what is the real problem to solve, since your current attempted solution path is just broken,

---

<div class="post-metadata">

**Author:** ![passerby](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@passerby](https://forums.freertos.org/u/passerby)\
**Post date:** [August 10, 2023, 5:22am UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962/10 "2023-08-10T05:22:16Z")

</div>

Hi @richard-damon :

I can also to change a way to solve this prolem . It’s just that the phenomenon is puzzling , My understanding of the critical is that it will stop scheduling.

Thank you for your help @richard-damon @ActoryOu @ydhuang .

Thanks.

---

<div class="post-metadata">

**Author:** ![ydhuang](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/ydhuang/32/3763_2.png) [@ydhuang](https://forums.freertos.org/u/ydhuang)\
**Post date:** [August 10, 2023, 3:31pm UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962/11 "2023-08-10T15:31:43Z")

</div>

The critical section is used to make any **application code** inside the section run without being interrupted (it disables interrupts). Yes, it does suspend the scheduler but that is _not_ its purpose. Like @richard-damon says, calling FreeRTOS API functions within a critical section is undefined behavior, so you should not expect the `xTaskCreate()` call within the critical section to do what you expect it to do. If you just want to suspend the scheduler, maybe `vTaskSuspendAll()` and `vTaskResumeAll()` would be a better choice?

---

<div class="post-metadata">

**Author:** ![RAc](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rac/32/1389_2.png) [@RAc](https://forums.freertos.org/u/RAc)\
**Post date:** [August 10, 2023, 3:39pm UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962/12 "2023-08-10T15:39:38Z")

</div>

As others mentioned, you may be falling victim to an unordered sequence of I/O calls. printf() is not reliable. Instead of it, use something like (Pseudo code)

unsigned char g\_Events[10];  
unsigned long g\_EventCounter;

task1:  
…  
g\_Events[g\_EventCounter++]=‘1’;  
…  
task2:  
…  
g\_Events[g\_EventCounter++]=‘2’;  
…

then evaluate the array posthumously.

---

<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:** [August 10, 2023, 5:19pm UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962/13 "2023-08-10T17:19:03Z")

</div>

Depending on the exact port, critical sections might NOT stop the scheduler, that only happens for ports that you an interrupt that gets masked by the critical section to invoke the schedulers. Other ports use a synchronous trap or a direct call which is NOT blocked by the critical section. A scheduling event won’t happen on its own on those, because no interrupt that could cause such a request can happen, but calling an API function still might cause one.

Basically, breaking the rules should only be done if you know what you are doing, and know things will behave the way you want (and keep up on possible changes to that behavior with updates).

---

<div class="post-metadata">

**Author:** ![passerby](https://avatars.discourse-cdn.com/v4/letter/p/a88e57/32.png) [@passerby](https://forums.freertos.org/u/passerby)\
**Post date:** [August 11, 2023, 2:36am UTC](https://forums.freertos.org/t/question-about-critical-and-create-a-new-task/17962/14 "2023-08-11T02:36:36Z")

</div>

Thanks! Help me a lot! 😀
