xSemaphoreGive
[信号量]
semphr. h
xSemaphoreGive( SemaphoreHandle_t xSemaphore );
用于释放信号量的宏。该信号量此前必须
通过调用 xSemaphoreCreateBinary()、xSemaphoreCreateMutex() 或
xSemaphoreCreateCounting() 创建。
不得在 ISR 中使用此宏。请参阅 xSemaphoreGiveFromISR (),了解可以从 ISR 中使用的替代方案。
此宏不得用于由
xSemaphoreCreateRecursiveMutex() 创建的信号量。
- 参数:
-
xSemaphore | 要释放的信号量的句柄。这是创建信号量时返回的句柄。 |
- 返回:
- 如果信号量释放成功,则返回 pdTRUE;如果发生错误,则返回 pdFALSE。信号量的实现基于队列。发布消息时,如果队列上没有空间,那么可能会发生错误,这表明最初未能正确获取信号量。
用法示例:
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.
|