Multithreaded Programming Guide

Decrement a Semaphore Count

Use sema_trywait(3C) to atomically decrement the count in the semaphore pointed to by sp when the count is greater than zero. This function is a nonblocking version of sema_wait().

sema_trywait Syntax

#include <thread.h>

int sema_trywait(sema_t *sp);

sema_trywait Return Values

sema_trywait() returns 0 if successful. When any of the following conditions is detected, sema_trywait() fails and returns the corresponding value.



sp refers to an invalid semaphore.



The semaphore pointed to by sp has a zero count.