programmer should not use s_thread_sem_* calls, becouse they are designed to be used internally - especially for mutex and condition variable emulation -, and not supported for every operating system.
s_thread_mutex_t *mut; s_thread_cond_t *cond; void * thread_function (void *arg) { int quit = 0; int *val = (int *) arg; while (!quit) { s_thread_mutex_lock(mut); if (*val == 0) { quit = 1; } else { *val = *val + 1; } s_thread_cond_signal(cond); s_thread_mutex_unlock(mut); sleep(3); } return NULL; } { int arg; int barg; int quit = 0; s_thread_t *tid; arg = 1; if (s_thread_mutex_init(&mut)) { // error } if (s_thread_cond_init(&cond)) { // error } if (s_thread_mutex_lock(mut)) { // error } tid = s_thread_create(&thread_function, &arg); if (tid == NULL) { // error } barg = arg; if (s_thread_mutex_unlock(mut)) { // error } while (!quit) { if (s_thread_mutex_lock(mut)) { // error } while (barg == arg) { if (s_thread_cond_wait(cond, mut)) { // error } } if (arg == 10) { arg = 0; quit = 1; } barg = arg; if (s_thread_mutex_unlock(mut)) { // error } } if (s_thread_mutex_destroy(mut)) { // error } if (s_thread_cond_destroy(cond)) { // error } }
Functions | |
| int | s_thread_sem_create (s_thread_sem_t **sem, int initial) |
| initialize the semaphore struct with an initial value | |
| int | s_thread_sem_destroy (s_thread_sem_t *sem) |
| destroys the given semaphore | |
| int | s_thread_sem_wait (s_thread_sem_t *sem) |
| waits on given semaphore | |
| int | s_thread_sem_wait_timeout (s_thread_sem_t *sem, int msec) |
| waits on given semaphore with timeout | |
| int | s_thread_sem_post (s_thread_sem_t *sem) |
| unlocks given semaphore | |
| int | s_thread_mutex_init (s_thread_mutex_t **mut) |
| initialize the mutex struct | |
| int | s_thread_mutex_destroy (s_thread_mutex_t *mut) |
| destroys the given mutex | |
| int | s_thread_mutex_lock (s_thread_mutex_t *mut) |
| locks the given mutex | |
| int | s_thread_mutex_trylock (s_thread_mutex_t *mut) |
| tries to lock the given mutex | |
| int | s_thread_mutex_unlock (s_thread_mutex_t *mut) |
| unlocks the given mutex | |
| int | s_thread_cond_init (s_thread_cond_t **cond) |
| initialize the condition variable struct | |
| int | s_thread_cond_destroy (s_thread_cond_t *cond) |
| destroys the given condition variable | |
| int | s_thread_cond_signal (s_thread_cond_t *cond) |
| signals waiter on given condition variable | |
| int | s_thread_cond_broadcast (s_thread_cond_t *cond) |
| signals all waiters on given condition variable | |
| int | s_thread_cond_wait (s_thread_cond_t *cond, s_thread_mutex_t *mut) |
| waits on condition variable | |
| int | s_thread_cond_timedwait (s_thread_cond_t *cond, s_thread_mutex_t *mut, int msec) |
| waits on condition variable, with timeout | |
| s_thread_t * | s_thread_create (void *(*f)(void *), void *farg) |
| creates a new thread of control that executes concurrently with the calling thread. | |
| int | s_thread_cancel (s_thread_t *tid) |
| cancels the thread. | |
| int | s_thread_join (s_thread_t *tid, void **ret) |
| suspends the execution of the calling thread until the thread identified by tid terminates, either by calling s_thread_exit or by being cancelled. | |
| int | s_thread_self (void) |
| returns the thread identifier for the calling thread. | |
| void | s_thread_exit (void *ret) |
| terminates the execution of the calling thread. | |
| int s_thread_cancel | ( | s_thread_t * | tid | ) |
cancels the thread.
| *tid | - thread id of the thread to cancel. |
| int s_thread_cond_broadcast | ( | s_thread_cond_t * | cond | ) |
signals all waiters on given condition variable
| *cond | - address of the condition variable pointer. |
| int s_thread_cond_destroy | ( | s_thread_cond_t * | cond | ) |
destroys the given condition variable
| *cond | - address of the condition variable pointer. |
| int s_thread_cond_init | ( | s_thread_cond_t ** | cond | ) |
initialize the condition variable struct
| **cond | - address of the condition variable pointer. |
| int s_thread_cond_signal | ( | s_thread_cond_t * | cond | ) |
signals waiter on given condition variable
| *cond | - address of the condition variable pointer. |
| int s_thread_cond_timedwait | ( | s_thread_cond_t * | cond, | |
| s_thread_mutex_t * | mut, | |||
| int | msec | |||
| ) |
waits on condition variable, with timeout
| *cond | - address of the condition variable pointer. | |
| *mut | - address of the mutex pointer. | |
| *msec | - timeout value in mili seconds. |
| int s_thread_cond_wait | ( | s_thread_cond_t * | cond, | |
| s_thread_mutex_t * | mut | |||
| ) |
waits on condition variable
| *cond | - address of the condition variable pointer. | |
| *mut | - address of the mutex pointer. |
| s_thread_t* s_thread_create | ( | void *(*)(void *) | f, | |
| void * | farg | |||
| ) |
creates a new thread of control that executes concurrently with the calling thread.
The new thread applies the function passing it as first argument.
| *f | - pointer to the function. | |
| *farg | - argument to pass to function. |
| int s_thread_join | ( | s_thread_t * | tid, | |
| void ** | ret | |||
| ) |
suspends the execution of the calling thread until the thread identified by tid terminates, either by calling s_thread_exit or by being cancelled.
| *tid | - thread id of the thread to cancel. | |
| **ret | - return value of the thread. |
| int s_thread_mutex_destroy | ( | s_thread_mutex_t * | mut | ) |
destroys the given mutex
| *mut | - address of the mutex pointer. |
| int s_thread_mutex_init | ( | s_thread_mutex_t ** | mut | ) |
initialize the mutex struct
| **mut | - address of the mutex pointer. |
| int s_thread_mutex_lock | ( | s_thread_mutex_t * | mut | ) |
locks the given mutex
| *mut | - address of the mutex pointer. |
| int s_thread_mutex_trylock | ( | s_thread_mutex_t * | mut | ) |
tries to lock the given mutex
| *mut | - address of the mutex pointer. |
| int s_thread_mutex_unlock | ( | s_thread_mutex_t * | mut | ) |
unlocks the given mutex
| *mut | - address of the mutex pointer. |
| int s_thread_self | ( | void | ) |
returns the thread identifier for the calling thread.
| int s_thread_sem_create | ( | s_thread_sem_t ** | sem, | |
| int | initial | |||
| ) |
initialize the semaphore struct with an initial value
| **sem | - address of the semaphore pointer. | |
| initial | - initial value. |
| int s_thread_sem_destroy | ( | s_thread_sem_t * | sem | ) |
destroys the given semaphore
| *sem | - address of the semaphore pointer. |
| int s_thread_sem_post | ( | s_thread_sem_t * | sem | ) |
unlocks given semaphore
| *sem | - address of the semaphore pointer. |
| int s_thread_sem_wait | ( | s_thread_sem_t * | sem | ) |
waits on given semaphore
| *sem | - address of the semaphore pointer. |
| int s_thread_sem_wait_timeout | ( | s_thread_sem_t * | sem, | |
| int | msec | |||
| ) |
waits on given semaphore with timeout
| *sem | - address of the semaphore pointer. | |
| msec | - timeout in mili seconds. |
1.4.7