建立新 thread int pthread_create(pthread_t *thread, const pthread_attr_t *屬性, void *(*函式)(void *), void *參數); 等待 thread 結束 int pthread_join(pthread_t thread, void **回傳值指標); ============================================================================== detach 一個現有的 thread int pthread_detach(pthread_t thread); 在建立 thread 時設定為 detached pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); (接著執行 pthread_create) ============================================================================== 立即結束目前的 thread void pthread_exit(void *回傳值); ============================================================================== cancel 一個現有的 thread int pthread_cancel(pthread_t thread); 設定目前的 thread 是否可 cancel int pthread_setcancelstate(int state, int *oldstate); state 參數: PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_DISABLE 設定目前的 thread 被 cancel 時的方式 int pthread_setcanceltype(int type, int *oldtype); type 參數: PTHREAD_CANCEL_DEFERRED 只有在 cancellation point 可 cancel PTHREAD_CANCEL_ASYNCHRONOUS 任何時候均可 cancel 在目前的 thread 設定 cancellation point void pthread_testcancel(void); 註:Cancellation Points (資料來源:FreeBSD Manual - PTHREAD_TESTCANCEL(3)) Cancellation points will occur when a thread is executing the following functions: close(), creat(), fcntl(), fsync(), msync(), nanosleep(), open(), pause(), pthread_cond_timedwait(), pthread_cond_wait(), pthread_join(), pthread_testcancel(), read(), sigwaitinfo(), sigsuspend(), sigwait(), sleep(), system(), tcdrain(), wait(), waitpid(), write(). ============================================================================== 將函式 push 進入目前 thread 的 cleanup routine stack void pthread_cleanup_push(void (*cleanup_routine)(void *), void *arg); 將函式從目前 thread 的 cleanup routine stack 給 pop 出來 void pthread_cleanup_pop(int execute); execute != 0 會執行該函式 execute == 0 只單純進行 pop,不執行該函式 ============================================================================== 初始化 mutex 變數 pthread_mutex_t mutex; pthread_mutex_init (&mutex, NULL); -- 或用 -- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; -- 以下是原函式的宣告 -- int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); 鎖定與解鎖 mutex pthread_mutex_lock(&mutex); pthread_mutex_unlock(&mutex); 鎖定 mutex,但如果已被鎖定,不等待鎖定解除 pthread_mutex_trylock(&mutex); ============================================================================== (此段落並非 pthread 函式庫的一部分) 初始化 semaphore sem_t semaphore; sem_init(&semaphore, 0, 0); -- 以下是原函式的宣告 -- int sem_init(sem_t *sem, int pshared, unsigned int value); pshared 參數:0 表示程序內,其他數字表示可跨程序(不支援?) value 參數:semaphore 的初始值 將 semaphore 的數值 + 1 (post) int sem_post(sem_t *sem); 將 semaphore 的數值 - 1 (wait) int sem_wait(sem_t *sem); 將 semaphore 的數值 - 1,但只有在數值非 0 時才會執行 (wait) int sem_trywait(sem_t *sem); ============================================================================== 初始化 conditional variable pthread_cont_init(&cond, NULL); -- 以下是原函式的宣告 -- int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); 等待於 conditional variable int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); 此函式除了等待於 conditional variable,也會同時解鎖 mutex 當被 signal 時,解除等待 conditional variable 並同時鎖定 mutex 解除等待一個等待於 conditional variable 的 thread int pthread_cond_signal(pthread_cond_t *cond); 解除等待所有等待於 conditional variable 的 thread int pthread_cond_broadcast(pthread_cond_t *cond);