FreeRTOS Support Archive
The FreeRTOS support forum is used to obtain active support directly from Real
Time Engineers Ltd. In return for using our top quality software and services for
free, we request you play fair and do your bit to help others too! Sign up
to receive notifications of new support topics then help where you can.
This is a read only archive of threads posted to the FreeRTOS support forum.
The archive is updated every week, so will not always contain the very latest posts.
Use these archive pages to search previous posts. Use the Live FreeRTOS Forum
link to reply to a post, or start a new support thread.
[FreeRTOS Home] [Live FreeRTOS Forum] [FAQ] [Archive Top] [February 2016 Threads]
Hello everyone,
My question is about the semaphore example here: http://www.freertos.org/a00122.html
Can the CreateMutex and xSemaphoreTake be in the same task.
I have have two task which potentially access to the same structure at the same time. Do I need to create a third task for the create mutex, or can the creation be in the task which handle the object, or in the main ?
Thank for your responses,
BR,
Flo
I'm not fully understanding your explanation of why you think it might not be ok - but a single task can create and use multiple semaphores and mutexes, and the semaphores and mutexes can be used by any number of other tasks. Mutual exclusion is taken care of inside the API functions - so there is no need to take care of that in your application code.
Thanks for your reply. You almost answer to my question.
a single task can create and use multiple semaphores and mutexes.
Can the same mutex be create and then used in the same task ?
To illustrate my point, I modified the example code from the previous link into what i'd like to do:
~~~~
SemaphoreHandle_t xSemaphore = NULL;
/* A task that creates a semaphore. /
void vATask( void * pvParameters )
{
/ Create the semaphore to guard a shared resource. As we are using
the semaphore for mutual exclusion we create a mutex semaphore
rather than a binary semaphore. */
xSemaphore = xSemaphoreCreateMutex();
/* .... */
if( xSemaphore != NULL )
{
/* See if we can obtain the semaphore. If the semaphore is not
available wait 10 ticks to see if it becomes free. */
if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
{
/* We were able to obtain the semaphore and can now access the
shared resource. */
/* ... */
/* We have finished accessing the shared resource. Release the
semaphore. */
xSemaphoreGive( xSemaphore );
}
else
{
/* We could not obtain the semaphore and can therefore not access
the shared resource safely. */
}
}
}
/* A task that uses the semaphore. /
void vAnotherTask( void * pvParameters )
{
/ ... Do other things. */
if( xSemaphore != NULL )
{
/* See if we can obtain the semaphore. If the semaphore is not
available wait 10 ticks to see if it becomes free. */
if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
{
/* We were able to obtain the semaphore and can now access the
shared resource. */
/* ... */
/* We have finished accessing the shared resource. Release the
semaphore. */
xSemaphoreGive( xSemaphore );
}
else
{
/* We could not obtain the semaphore and can therefore not access
the shared resource safely. */
}
}
}
~~~~
Thanks.
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.