Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Updated Mar 2025

xSemaphoreCreateCountingStatic

[Semaphores]

TIP: 'Task Notifications' can provide a light weight alternative to counting semaphores in many situations

semphr. h

1SemaphoreHandle_t xSemaphoreCreateCountingStatic(
2 UBaseType_t uxMaxCount,
3 UBaseType_t uxInitialCount
4 StaticSemaphore_t *pxSemaphoreBuffer );

Creates a counting semaphore and returns a handle by which the newly created semaphore can be referenced. configSUPPORT_STATIC_ALLOCATION must be set to 1 in FreeRTOSConfig.h for this RTOS API function to be available.

Each counting semaphore require a small amount of RAM that is used to hold the semaphore's state. If a counting semaphore is created using xSemaphoreCreateCounting() then the required RAM is automatically allocated from the FreeRTOS heap. If a counting semaphore is created using xSemaphoreCreateCountingStatic() then the RAM is provided by the application writer, which requires an additional parameter, but allows the RAM to be statically allocated at compile time. See the Static Vs Dynamic allocation page for more information.

Counting semaphores are typically used for two things:

  1. Counting events.

    In this usage scenario an event handler will 'give' a semaphore each time an event occurs (incrementing the semaphore count value), and a handler task will 'take' a semaphore each time it processes an event (decrementing the semaphore count value). The count value is therefore the difference between the number of events that have occurred and the number that have been processed. In this case it is desirable for the initial count value to be zero.

    Note the same functionality can often be achieved in a more efficient way using a direct to task notification.

  2. Resource management.

    In this usage scenario the count value indicates the number of resources available. To obtain control of a resource a task must first obtain a semaphore - decrementing the semaphore count value. When the count value reaches zero there are no free resources. When a task finishes with the resource it 'gives' the semaphore back - incrementing the semaphore count value. In this case it is desirable for the initial count value to be equal to the maximum count value, indicating that all resources are free.

Parameters:

  • uxMaxCount

    The maximum count value that can be reached. When the semaphore reaches this value it can no longer be 'given'.

  • uxInitialCount

    The count value assigned to the semaphore when it is created.

  • pxSemaphoreBuffer

    Must point to a variable of type StaticSemaphore_t, which is then used to hold the semaphore's data structures.

Returns:

If the semaphore is created successfully then a handle to the semaphore is returned. If pxSemaphoreBuffer is NULL then NULL is returned.

Example usage:

1static StaticSemaphore_t xSemaphoreBuffer;
2
3void vATask( void * pvParameters )
4{
5SemaphoreHandle_t xSemaphore;
6
7 /* Create a counting semaphore that has a maximum count of 10 and an
8 initial count of 0. The semaphore's data structures are stored in the
9 xSemaphoreBuffer variable - no dynamic memory allocation is performed. */
10 xSemaphore = xSemaphoreCreateCountingStatic( 10, 0, &xSemaphoreBuffer );
11
12 /* pxSemaphoreBuffer was not NULL so it is expected that the semaphore
13 will be created. */
14 [configASSERT](/Documentation/02-Kernel/03-Supported-devices/02-Customization/#configassert)( xSemaphore );
15}