SemaphoreHandle_t xSemaphore = NULL; 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 ) { if( xSemaphoreGive( xSemaphore ) != pdTRUE ) { // We would expect this call to fail because we cannot give // a semaphore without first "taking" it! } // Obtain the semaphore - don't block if the semaphore is not // immediately available. if( xSemaphoreTake( xSemaphore, ( TickType_t ) 0 ) ) { // We now have the semaphore and can access the shared resource. // ... // We have finished accessing the shared resource so can free the // semaphore. if( xSemaphoreGive( xSemaphore ) != pdTRUE ) { // We would not expect this call to fail because we must have // obtained the semaphore to get here. } } } }
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.
|
Latest News
FreeRTOS kernel V10.0.1 is available for immediate download. Now MIT licensed. Video: Watch James Gosling & Richard Barry at re:Invent, Las Vegas 2017. New FAQ page about the FreeRTOS kernel and Amazon FreeRTOS. FreeRTOS Partners
|