.线ç¨çåºæ¬ä»ç»
(1)线ç¨çæ¦è¿°
线ç¨ä¸è¿ç¨ç±»ä¼¼ï¼ä¹å
许åºç¨ç¨åºå¹¶åæ§è¡å¤ä¸ªä»»å¡çä¸ç§æºå¶ãä¸ä¸ªè¿ç¨å¯ä»¥å
å«å¤ä¸ªçº¿ç¨ï¼åä¸ç¨åºä¸çææ线ç¨å
±äº«åä¸ä»½å
¨å±å
ååºåï¼çº¿ç¨ä¹é´æ²¡æçæ£æä¹çç级ä¹åãåä¸ä¸ªè¿ç¨ä¸ç线ç¨å¯ä»¥å¹¶åæ§è¡ï¼å¦æå¤çå¨æ¯å¤æ ¸çè¯çº¿ç¨ä¹å¯ä»¥å¹¶è¡æ§è¡ï¼å¦æä¸ä¸ªçº¿ç¨å 为çå¾
I/Oæä½èé»å¡ï¼é£ä¹å
¶ä»çº¿ç¨ä¾ç¶å¯ä»¥ç»§ç»è¿è¡
(2)线ç¨ä¼äºè¿ç¨çæ¹é¢
argv,environ
主线ç¨æ
线ç¨3çæ
线ç¨2çæ
线ç¨1çæ
å
±äº«å½æ°åºå
±äº«çå
å
å
æªåå§åçæ°æ®æ®µ
åå§åæ°æ®æ®µ
ææ¬
.è¿ç¨é´çä¿¡æ¯é¾ä»¥å
±äº«ãç±äºé¤å»åªè¯»ä»£ç 段å¤ï¼ç¶åè¿ç¨å¹¶æªå
±äº«å
åï¼å æ¤å¿
é¡»éç¨ä¸äºè¿ç¨é´é讯ï¼å¨è¿ç¨ä¹é´äº¤æ¢ä¿¡æ¯
.è°ç¨fork()æ¥å建è¿ç¨ä»£ä»·ç¸å¯¹è¾é«
线ç¨å¾å¥½ç解å³äºä¸è¿°ä¿©ä¸ªé®é¢
.线ç¨ä¹é´è½å¤æ¹ä¾¿ï¼å¿«éçå
±äº«ä¿¡æ¯ï¼åªéå°æ°æ®å¤å¶å°å
±äº«ï¼å
¨å±æå ï¼åéä¸å³å¯
.å建线ç¨æ¯å建线ç¨é常è¦å¿«10çè³æ´å¤ï¼çº¿ç¨å建ä¹æ以快ï¼æ¯å 为forkå建è¿ç¨æ¶æéå¤å¶å¤ä¸ªå±æ§ï¼èå¨çº¿ç¨ä¸ï¼è¿äºå±æ§æ¯å
±äº«çã
(3)å建线ç¨
å¯å¨ç¨åºæ¶ï¼äº§ççè¿ç¨åªæåæ¡çº¿ç¨ï¼æ们称ä¹ä¸ºä¸»çº¿ç¨
#include<pthread.h>
int pthread_create(pthread_t *thread,const pthread_attr_t *attr,void*(*start)(void *),void *arg);12
æ°çº¿ç¨éè¿è°ç¨å¸¦æargçå½æ°å¼å§æ§è¡ï¼è°ç¨pthread_create()ç线ç¨ä¼ç»§ç»æ§è¡è¯¥è°ç¨ä¹åçè¯å¥ã
(4)ç»æ¢çº¿ç¨
å¯ä»¥ä»¥å¦ä¸æ¹å¼ç»æ¢çº¿ç¨çè¿è¡
.线ç¨è°ç¨pthread_exit()
.线ç¨startå½æ°æ§è¡returnè¯å¥å¹¶è¿åæå®å¼
.è°ç¨pthread_cancel()åæ¶çº¿ç¨
.ä»»æ线ç¨è°ç¨äºexit(),æè
主线ç¨æ§è¡äºreturnè¯å¥ï¼é½ä¼å¯¼è´è¿ç¨ä¸çææ线ç¨ç«å³ç»æ¢
pthread_exit()å½æ°å¯ä»¥ç»æ¢çº¿ç¨ï¼ä¸å
¶è¿åå¼å¯ç±å¦ä¸çº¿ç¨éè¿è°ç¨pthread_join()è·å¾
#include<pthread.h>void pthread_exit(void *retval);12
è°ç¨pthread_exit()ç¸å½äºå¨çº¿ç¨çstartå½æ°ä¸æ§è¡return,ä¸åä¹å¤å¨äºï¼pthread_exit()å¯ä»¥å¨ä»»ä½å°æ¹è°ç¨ï¼åæ°retvalæå®äºçº¿ç¨çè¿åå¼
(5)è·å线ç¨ID
#include<pthread.h>pthread_t pthread_self(void);12
线ç¨IDå¨åºç¨ç¨åºä¸ä¸»è¦æå¦ä¸ç¨é
.ä¸åçpthreadså½æ°å©ç¨çº¿ç¨IDæ¥æ è¯è¦æä½ç®æ 线ç¨ã
.å¨å
·ä½çåºç¨ç¨åºä¸ï¼ä»¥ç¹å®çº¿ç¨ç线ç¨IDä½ä¸ºå¨ææ°æ®ç»æçæ ç¾ï¼è¿é¢æç¨å¤ï¼æ¢å¯ç¨æ¥è¯å«æ个æ°æ®ç»æçå建è
æå±ä¸»çº¿ç¨ï¼åå¯ç¡®å®éå对该æ°æ®ç»ææ§è¡æä½çå
·ä½çº¿ç¨
å½æ°pthread_equal()å¯æ£æ¥ä¿©ä¸ªçº¿ç¨çIDæ¯å¦ç¸å
#include<pthread.h>int pthread_equal(pthread_t t1,pthread_t t2);//å¦æç¸åè¿åé0å¼ï¼å¦åè¿å0123
(6)è¿æ¥å·²ç»æ¢ç线ç¨
å½æ°pthread_join()çå¾
ç±thread表è¯ç线ç¨ç»æ¢
#include<pthread.h>int pthread_join(pthread_t thread,void **retval);//è¿å0è°ç¨æåï¼å¦å失败123
å¦æpthread_join()ä¼ å
¥ä¸ä¸ªä¹åå·²ç¶è¿æ¥è¿ç线ç¨IDï¼å°ä¼å¯¼è´æ æ³é¢ç¥çè¡ä¸ºï¼å½ç¸å线ç¨IDå¨åä¸ä¸æ¬¡è¿æ¥åæ°å¥½ä¸ºå¦ä¸æ°å»ºçº¿ç¨æéç¨ï¼å度è¿æ¥çå¯è½å°±æ¯è¿ä¸ªæ°çº¿ç¨
è¥çº¿ç¨æªå离ï¼åå°±åºè¯¥ä½¿ç¨pthread_join()æ¥è¿æ¥çº¿ç¨ï¼å¦åä¼äº§çåµå°¸çº¿ç¨
pthrea_join()å½æ°çè¦ç¹
.线ç¨ä¹é´çå
³ç³»æ¯å¯¹ççï¼æ以任æ线ç¨é½å¯ä»¥è°ç¨pthread_join()æ¥è¿æ¥å
¶ä»çº¿ç¨
.pthread_join()æ æ³é对任æ线ç¨ï¼åªè½è¿æ¥å个线ç¨
(6)线ç¨çå离
é»è®¤æ
åµä¸çº¿ç¨é½æ¯å¯è¿æ¥çï¼ä½ææ¶åï¼æ们并ä¸å
³å¿çº¿ç¨éåºçç¶æï¼æ们å¯ä»¥è°ç¨pthread_detach()并åthreadåæ°ä¼ å
¥æå®çº¿ç¨ççæ è¯ç¬¦ï¼å°è¯¥çº¿ç¨æ 记为å¤äºå离ç¶æ
#include<pthread.h>int pthread_detach(pthread_t thread);//è¿å0æåï¼å¦å失败123
ä¸æ¦çº¿ç¨å¤äºå离ç¶æï¼å°±ä¸è½å¨ä½¿ç¨pthread_join()æ¥è·åå
¶ç¶æï¼ä¹æ æ³ä½¿å
¶éè¿å¯è¿æ¥ç¶æ
(7)å¨åºç¨ç¨åºä¸å¦ä½æ¥éæ©è¿ç¨è¿æ¯çº¿ç¨
.线ç¨ä¹é´å
±äº«æ°æ®å¾ç®åï¼è¿ç¨é´çæ°æ®å
±äº«éè¦æ´å¤çæå
¥
.å建线ç¨è¦æ¯å建è¿ç¨åå¾å¤
.å¤çº¿ç¨ç¼ç¨æ¶ï¼éè¦ç¡®ä¿è°ç¨çº¿ç¨å®å
¨çå½æ°
.æ个线ç¨ä¸çbugå¯è½ä¼å±å®³è¿ç¨ä¸ææ线ç¨
.æ¯ä¸ªçº¿ç¨é½å¨å¾ç¨å®¿ä¸»è¿ç¨ä¸æéçèæå°å空é´
.å¨å¤çº¿ç¨åºç¨ä¸ï¼éè¦å°å¿ä½¿ç¨ä¿¡å·
.é¤äºæ°æ®ï¼çº¿ç¨è¿å¯ä»¥å
±äº«æ件æ述符ï¼ä¿¡å·å¤ç½®ï¼å½åå·¥ä½ç®å½ï¼ä»¥åç¨æ·IDåç»ID
线ç¨çåæ¥
(1)ä¿æ¤å
±äº«åé访é®ï¼äºæ¥é
线ç¨ç主è¦ä¼å¿å¨äºè½å¤éè¿å
¨å±åéæ¥å
±äº«ä¿¡æ¯ï¼ä¸è¿è¿ç§å
±äº«æ¯æ代价çãå¿
须确ä¿å¤ä¸ªçº¿ç¨ä¿®æ¹åä¸åéæ¶ï¼ä¸ä¼æå
¶ä»çº¿ç¨ä¹æ£å¨ä¿®æ¹æ¤åéï¼ä¸ºé¿å
线ç¨æ´æ°æ¶å
±äº«åéæ¶æåºç°çé®é¢ï¼å¿
须使ç¨äºæ¥éæ¥ç¡®ä¿åæ¶ä»
æä¸ä¸ªçº¿ç¨å¯ä»¥è®¿é®æ项å
±äº«èµæº
(2)éæåé
çäºæ¥é
äºæ¥éæ¢å¯ä»¥åéæåéé£æ ·åé
ï¼ä¹å¯ä»¥å¨è¿è¡æ¶å¨æåé
ï¼äºæ¥éå±äºpthread_mutex_tç±»åçåéï¼å¨ä½¿ç¨ä¹åå¿
须对å
¶åå§åã对äºéæåé
çäºæ¥éèè¨ï¼å¯å¦ä¸ä¾æ示ï¼å°PTHREAD_MUTEX_INITIALIZERèµç»äºæ¥é
pthread_mutex_t = PTHREAD_MUTEX_INITIALIZER;1
1.å éå解éäºæ¥é
åå§åä¹åï¼äºæ¥éå¤äºæªéå®ç¶æãå½æ°pthread_mutex_lock()å¯ä»¥éå®æä¸äºæ¥é
èå½æ°pthread_mutex_unlock()åå¯ä»¥å°ä¸ä¸ªäºæ¥é解é
#include<pthread.h>int pthread_mutex_lock(pthread_mutex_t *mutex);int pthread_mutex_unlock(pthread_mutex_t *mutex);//è¿å0æåï¼å
¶ä»å¤±è´¥1234
è¦éå®äºæ¥éï¼å¨è°ç¨pthread_mutex_lock()æ¶éè¦æå®äºæ¥éï¼å¦æäºæ¥éå½åå¤äºæªéå®ç¶æï¼å该è°ç¨å°ä¼ç«å³è¿åï¼å¦æ该äºæ¥é已被å
¶ä»çº¿ç¨éå®ï¼é£ä¹è¯¥è°ç¨å°ä¼é»å¡ï¼ç´è³äºæ¥é被解é
å½æ°pthread_mutex_unlock()å°è§£éä¹åå·²éè°ç¨çº¿ç¨éå®çäºæ¥é
2.äºæ¥éçæ§è½
é常æ
åµä¸ï¼çº¿ç¨ä¼è±è´¹æ´å¤çæ¶é´å»åå
¶ä»å·¥ä½ï¼å¯¹äºæ¥éçå é解éç¸å¯¹è¦å°çå¤ï¼å æ¤ä½¿ç¨äºæ¥é对大é¨åç¨åºæ¥è¯´æ§è½å¹¶æ æ¾èçå½±å
3.äºæ¥éçæ»é
å½ä¸ä¸ªçº¿ç¨éè¦åæ¶è®¿é®å¤ä¸ªå
±äº«èµæºæ¶ï¼æ²¡ä¸ªèµæºç±ä¸åçäºæ¥ç´¢ç®¡çãå½è¶
è¿ä¸ä¸ªçº¿ç¨å éåä¸ç»äºæ¥éæ¶ï¼å°±æå¯è½åçæ»éãå¦ä¸å¾æ示
线ç¨A
1.pthread_mutex_lock(mutex1);
2.pthread_mutex_lock(mutex2);
线ç¨2
1.pthread_mutex_lock(mutex2);
2.pthread_mutex_lock(mutex1);
æ¯ä¸ªçº¿ç¨é½æåçéä½ä¸ä¸ªäºæ¥éï¼æ¥çè¯å¾å¯¹ä»¥ä¸ºå¦ä¸çº¿ç¨éå®çäºæ¥éå éï¼å°±ä¼ä¸ç´çä¸å»
è¦é¿å
æ¤ç±»æ»éé®é¢ï¼æç®åçå°±æ¯å®ä¹äºæ¥éçå±çº§å
³ç³»
温馨提示:答案为网友推荐,仅供参考