古詩詞大全網 - 成語查詢 - C語言多線程的操作步驟

C語言多線程的操作步驟

線程創建

函數原型:intpthread_create(pthread_t*restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void *restrict arg);

返回值:若是成功建立線程返回0,否則返回錯誤的編號。

形式參數:pthread_t*restrict tidp要創建的線程的線程id指針;const pthread_attr_t *restrict attr創建線程時的線程屬性;void *(start_rtn)(void)返回值是void類型的指針函數;void *restrict arg start_rtn的形參。

線程掛起:該函數的作用使得當前線程掛起,等待另壹個線程返回才繼續執行。也就是說當程序運行到這個地方時,程序會先停止,然後等線程id為thread的這個線程返回,然後程序才會斷續執行。

函數原型:intpthread_join(pthread_tthread, void **value_ptr);

參數說明如下:thread等待退出線程的線程號;value_ptr退出線程的返回值。

返回值:若成功,則返回0;若失敗,則返回錯誤號。

線程退出

函數原型:voidpthread_exit(void *rval_ptr);

獲取當前線程id

函數原型:pthread_tpthread_self(void);

互斥鎖

創建pthread_mutex_init;銷毀pthread_mutex_destroy;加鎖pthread_mutex_lock;解鎖pthread_mutex_unlock。

條件鎖

創建pthread_cond_init;銷毀pthread_cond_destroy;觸發pthread_cond_signal;廣播pthread_cond_broadcast;等待pthread_cond_wait。