#include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int value); int sem_wait(sem_t *sem); /**//* P(sem), wait(sem) */ int sem_post(sem_t *sem); /**//* V(sem), signal(sem) */ int sem_getvalue(sem_t *sem, int*sval); int sem_trywait(sem_t *sem); int sem_destroy(sem_t *sem); /**//* undo sem_init() */ /**//* named semaphores - these are less useful here */ sem_t *sem_open( ); int sem_close(sem_t *sem); int sem_unlink(constchar*name);
互斥量
基本操作
#include <pthread.h> int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr); pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_destroy(pthread_mutex_t *mutex);
條件變量
一種信號機制
基本操作
#include <pthread.h> int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr); pthread_cond_t cond = PTHREAD_COND_INITIALIZER; int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); int pthread_cond_signal(pthread_cond_t *cond); int pthread_cond_timedwait( ); int pthread_cond_broadcast(pthread_cond_t *cond); int pthread_cond_destroy(pthread_cond_t *cond);