Showing results for 
Search instead for 
Did you mean: 

FreeRTOS Mutex example ?

Posted on April 03, 2017 at 00:53


Does anyone know where I can find a simple example for FreeRTOS on Mutex ?

I want to do two task but the first task never finishes and jump already to the second one because of the systick,

how can I make the first one finish and do the second one with Mutex ?

Thanks for reading and advicing

#multitasking #rtos #stm32f1xx #mutex

Note: this post was migrated and contained many threaded conversations, some content may be missing.
Associate III
Posted on April 03, 2017 at 06:17

Try the

 or better still the

Posted on April 03, 2017 at 11:14

Thanks for the link, I appreciate that, can you tell me which directory containing an example for semaphore in FreeRTOS9 directory ? thanks

ST Employee
Posted on April 03, 2017 at 16:55




In all STM32Cube packages, you will find an example called ''FreeRTOS_Mutexes''.

Just search it in the package you are using.

You can getmore details about mutexes in the

''Developing Applications on STM32Cube with RTOS'', sub-section ''Mutexes example''.

If you are using an STM32F4, the example ''

FreeRTOS_Mutexes'' is available as application example for boards marked with ''x'' in this table (extracted from




To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Associate II
Posted on April 03, 2017 at 21:12

I think use semaphore instead of mutex is easier.

SemaphoreHandle_t Sem1, Sem2;

int main(void) {

      /* Create semaphore. */

      Sem1 = xSemaphoreCreateBinary();

      Sem2 = xSemaphoreCreateBinary();

      .... Create task and run scheduler.


void Task1(void *param) {

      /* Give once for first time. */


      while(1) {


/* Wait for Sem1 */

            if(xSemaphoreTake(Sem1, portMAX_DELAY) == pdTURE) {

                  /* Your first task code */

                  /* Wake


second task. */





void Task2(void *param) {

      while(1) {

            /* Wait for Sem2 */

            if(xSemaphoreTake(Sem2, portMAX_DELAY) == pdTURE) {

 /* Your second task code */

                  /* Wake


 first task. */




Posted on April 04, 2017 at 00:23

Thank you very much for the advice.

Posted on April 04, 2017 at 08:58

That's good idea to try, thanks

Posted on April 05, 2017 at 05:51

What should I add on task2 if task 2 time is much longer than task 1 ? and task 2 time is much longer than default systick ?

It seemed that it's doing task 1 but failed on task 2 then stop forever ?


Posted on April 05, 2017 at 06:04

Did you read the chapter on mutexes in the book I posted a link to in my first reply?  In particular read the part about using mutexes from tight loops.

Posted on April 05, 2017 at 07:20

This chapter : 7.3 Mutexes (and Binary Semaphores) ?

Which part exactly ? May be I missed it ?