å¾æ©ä»¥åå°±æ³åålinuxä¸å¤çº¿ç¨ç¼ç¨åwindowsä¸çå¤çº¿ç¨ç¼ç¨äºï¼ä½æ¯æ¯å½åæ¶åä¸ç¥éä»åªä¸ªå°æ¹åèµ·ï¼ææ ·æèªå·±ç¥éçä¸è¥¿é½ååºæ¥ï¼ä¸é¢æå°±è°è°linuxå¤çº¿ç¨å线ç¨åæ¥ï¼å¹¶å°å®åwindowsçå¤çº¿ç¨è¿è¡æ¯è¾ï¼ççä»ä»¬ä¹é´æä»ä¹ç¸åç¹åä¸åçå°æ¹ã
å
¶å®æå¼å§ææ¯æwindowsä¸ç¼ç¨çï¼å
æ¬windowsç¼ç¨ï¼windows 驱å¨ï¼å
æ¬usb驱å¨ï¼ndis驱å¨,pci驱å¨ï¼1394驱å¨ççï¼åæ¶ä¹ä¸æ¡é¾æå¡ï¼åwindowsä¸çåºç¨ç¨åºå¼åï¼åé¢æ
¢æ
¢çæå对linuxå¼å产çæ¯è¾æ·±çå
´è¶£åç±å¥½ï¼å°±è½¬å°ælinuxå¼åäºãå¨æ¥ä¸æ¥çæè¿ä¼åä¸äºå客ï¼ä¸»è¦æ¯ålinuxç¼ç¨åwindowsç¼ç¨çåºå«å§ï¼ç°å¨æ³åçæ¯linuxä¸usb驱å¨åwindowsä¸usb驱å¨å¼åçåºå«ï¼è¿äºé½æ¯åè¯ï¼çæå°linuxå¤çº¿ç¨åwindowså¤çº¿ç¨è®²è§£å®åï¼æååä¸ç¯usb驱å¨ï¼è°è°windows ålinux usb驱å¨çä¸ä¸ã好äºï¼è¨å½æ£ä¼ ãå¼å§å°å¤çº¿ç¨äºã
é¦å
æ们讲讲为ä»ä¹è¦éç¨å¤çº¿ç¨ç¼ç¨ï¼å
¶å®å¹¶ä¸æ¯ææçç¨åºé½å¿
é¡»éç¨å¤çº¿ç¨ï¼æäºæ¶åéç¨å¤çº¿ç¨ï¼æ§è½è¿æ²¡æå线ç¨å¥½ãæ以æ们è¦ææ¸
æ¥ï¼ä»ä¹æ¶åéç¨å¤çº¿ç¨ãéç¨å¤çº¿ç¨ç好å¤å¦ä¸ï¼
(1)å 为å¤çº¿ç¨å½¼æ¤ä¹é´éç¨ç¸åçå°å空é´ï¼å
±äº«å¤§é¨åçæ°æ®ï¼è¿æ ·åå¤è¿ç¨ç¸æ¯ï¼ä»£ä»·æ¯è¾èä¿ï¼å 为å¤è¿ç¨çè¯ï¼å¯å¨æ°çè¿ç¨å¿
é¡»åé
ç»å®ç¬ç«çå°å空é´ï¼è¿æ ·éè¦æ°æ®è¡¨æ¥ç»´æ¤ä»£ç 段ï¼æ°æ®æ®µåå æ 段ççã
(2)å¤çº¿ç¨åå¤è¿ç¨ç¸æ¯ï¼ä¸ä¸ªææ¾çä¼ç¹å°±æ¯çº¿ç¨ä¹é´çéä¿¡äºï¼å¯¹ä¸åè¿ç¨æ¥è¯´ï¼å®ä»¬å
·æç¬ç«çæ°æ®ç©ºé´ï¼è¦è¿è¡æ°æ®çä¼ éåªè½éè¿éä¿¡çæ¹å¼è¿è¡ï¼è¿ç§æ¹å¼ä¸ä»
è´¹æ¶ï¼èä¸å¾ä¸æ¹ä¾¿ãä½æ¯å¯¹äºå¤çº¿ç¨å°±ä¸ä¸æ ·äºãä»ä»¬ä¹é´å¯ä»¥ç´æ¥å
±äº«æ°æ®ï¼æ¯å¦æç®åçæ¹å¼å°±æ¯å
±äº«å
¨å±åéãä½æ¯å
±äº«å
¨é¨åéä¹è¦æ³¨æå¦ï¼åµåµï¼å¿
须注æåæ¥ï¼ä¸ç¶åæä½ ç¥éçãåµåµã
(3)å¨å¤cpuçæ
åµä¸ï¼ä¸åç线ç¨å¯ä»¥è¿è¡ä¸åçcpuä¸ï¼è¿æ ·å°±å®å
¨å¹¶è¡äºã
åæ£æè§å¾å¨è¿ç§æ
åµä¸ï¼éç¨å¤çº¿ç¨æ¯è¾çæ³ãæ¯å¦è¯´ä½ è¦åä¸ä¸ªä»»å¡å2个æ¥éª¤ï¼ä½ 为æé«å·¥ä½æçï¼ä½ å¯ä»¥å¤çº¿ç¨ææ¯ï¼å¼è¾2个线ç¨ï¼ç¬¬ä¸ä¸ªçº¿ç¨å°±å第ä¸æ¥çå·¥ä½ï¼ç¬¬2个线ç¨å°±å第2æ¥çå·¥ä½ãä½æ¯ä½ è¿ä¸ªæ¶åè¦æ³¨æåæ¥äºãå 为åªæ第ä¸æ¥åå®æè½å第2æ¥çå·¥ä½ãè¿æ¶ï¼æ们å¯ä»¥éç¨åæ¥ææ¯è¿è¡çº¿ç¨ä¹é´çéä¿¡ã
é对è¿ç§æ
åµï¼æ们é¦å
讲讲å¤çº¿ç¨ä¹é´çéä¿¡ï¼å¨windowså¹³å°ä¸ï¼å¤çº¿ç¨ä¹é´éä¿¡éç¨çæ¹æ³ä¸»è¦æï¼
(1)å
±äº«å
¨å±åé,è¿ç§æ¹æ³æ¯æ容ææ³å°çï¼åµåµï¼é£å°±é¦å
讲讲å§ï¼æ¯å¦è¯´å§ï¼ä¸é¢çé®é¢ï¼ç¬¬ä¸æ¥è¦å第2æ¥ä¼ éæ¶æ®ï¼æ们å¯ä»¥ä¹é´å
±äº«å
¨å±åéï¼è®©ä¸¤ä¸ªçº¿ç¨ä¹é´ä¼ éæ°æ®ï¼è¿æ¶ä¸»è¦èèçå°±æ¯åæ¥äºï¼å ä¸ºä½ åé¢ç线ç¨å¨å¯¹æ°æ®è¿è¡æä½çæ¶åï¼ä½ 第ä¸ä¸ªçº¿ç¨åæ¹åäºæ°æ®çå
容ï¼ä½ ä¸åæ¥ä¿æ¤ï¼åæå¾ä¸¥éçãä½ ä¹ç¥éï¼è¿ç§æ
åµå°±æ¯è¯»èæ°æ®äºãå¨è¿ç§æ
åµä¸ï¼æ们æ容ææ³å°çåæ¥æ¹æ³å°±æ¯è®¾ç½®ä¸ä¸ªbool flagäºï¼æ¯å¦è¯´å¨ç¬¬2个线ç¨è¿æ²¡æç¨å®æ°æ®åï¼ç¬¬ä¸ä¸ªçº¿ç¨ä¸è½åå
¥ãææ¶å¨2个线ç¨æéçæ¶é´ä¸ç¸åçæ¶åï¼ææ ·è¾¾å°æ大æççåæ¥ï¼å°±æ¯è¾éº»ç¦äºãå±ä»¬å¯ä»¥å¤å¼å 个ç¼å²åºè¿è¡æä½ãå°±åç产è
æ¶è´¹è
ä¸æ ·äºãå¦ææ¯2个线ç¨ä¸ç´å¨è·çï¼ç±äºæ¶é´ä¸ä¸è´ï¼ç¼å²åºè¿æ©ä¼æº¢åºçãå¨è¿ç§æ
åµä¸å°±è¦èèäºï¼æ¯ä¸è®©æ°æ®åå
¥è¿æ¯è®©æ°æ®è¦çæèçæ°æ®ï¼è¿æ¶åå°±è¦å
·ä½é®é¢å
·ä½åæäºãå°±æ¤æä½ï¼åµåµãå°±æ¯ç¨boolåéæ§å¶åæ¥ï¼linux åwindowsæ¯ä¸æ ·çã
æ¢ç¶è®²éäºè¿éï¼å°±å讲讲å
¶å®åæ¥çæ¹æ³ãåæ · é对ä¸é¢çè¿ä¸ªé®é¢ï¼å
±äº«å
¨å±åéåæ¥é®é¢ãé¤äºéç¨boolåéå¤ï¼æ容ææ³å°çæ¹æ³å°±æ¯äºæ¥éäºãåµåµï¼ä¹å°±æ¯ä¼ 说ä¸çå éäºãwindowsä¸å éålinuxä¸å éæ¯ç±»ä¼¼çãéç¨äºæ¥éè¿è¡åæ¥ï¼è¦æ³è¿å
¥é£æ®µä»£ç ï¼å°±å
å¿
é¡»è·å¾äºæ¥éã
linuxä¸äºæ¥éçå½æ°æ¯ï¼
windowsä¸äºæ¥éçå½æ°æï¼createmutex å建ä¸ä¸ªäºæ¥éï¼ç¶åå°±æ¯è·å¾äºæ¥éwaitforsingleobjectå½æ°ï¼ç¨å®äºå°±éæ¾äºæ¥éReleaseMutex(hMutex)ï¼å½åå°0çæ¶å å
æ ¸ä¼æä¼éæ¾å
¶å¯¹è±¡ãä¸é¢æ¯windowsä¸ä¸äºæ¥çå 个å½æ°ååã
HANDLE WINAPI CreateMutex(
__in LPSECURITY_ATTRIBUTES lpMutexAttributes,
__in BOOL bInitialOwner,
__in LPCTSTR lpName
);
å¯ä»¥å¯ç¨æ¥å建ä¸ä¸ªæåææ åçäºæ¥é对象
第ä¸åæ° å¯ä»¥æåä¸ä¸ªç»æä½SECURITY_ATTRIBUTESãä¸è¬å¯ä»¥è®¾ä¸ºnullï¼
第äºåæ° æå½æ¶çå½æ°æ¯ä¸æ¯æåºæåºç¶æ FALSE为å½åæ¥æè
ä¸ä¼å建äºæ¥
第ä¸åæ° æææ¯å¦æ¯æåçäºæ¥å¯¹è±¡ å¦ææ¯æ å ç¨null就好ã
DWORD WINAPI WaitForSingleObject(
__in HANDLE hHandle,
__in DWORD dwMilliseconds
);
第ä¸ä¸ªæ¯ å建çäºæ¥å¯¹è±¡çå¥æã第äºä¸ªæ¯ 表示å°å¨å¤å°æ¶é´ä¹åè¿å å¦æ设为å®INFINITE åä¸ä¼è¿å ç´å°ç¨æ·èªå·±å®ä¹è¿åã
对äºlinuxæä½ç³»ç»ï¼äºæ¥ä¹æ¯ç±»ä¼¼çï¼åªæ¯å½æ°ä¸åç½¢äºãå¨linuxä¸ï¼åäºæ¥ç¸å
³çå 个å½æ°ä¹è¦éªäº®ç»åºäºã
pthread_mutex_initå½æ°ï¼åå§åä¸ä¸ªäºæ¥éï¼
pthread_mutex_destroyå½æ°ï¼æ³¨éä¸ä¸ªäºæ¥éï¼
pthread_mutex_lockå½æ°ï¼å éï¼å¦æä¸æåï¼é»å¡çå¾
ï¼
pthread_mutex_unlockå½æ°ï¼è§£éï¼
pthread_mutex_trylockå½æ°ï¼æµè¯å éï¼å¦æä¸æåå°±ç«å³è¿åï¼é误ç 为EBUSY;
è³äºè¿äºå½æ°çç¨æ³ï¼googleä¸ä¸æï¼å°±åºæ¥äºï¼åµåµï¼å¨è¿éä¸å¤è®²äºãwindowsä¸è¿æä¸ä¸ªå¯ä»¥ç¨æ¥ä¿æ¤æ°æ®çæ¹æ³ï¼ä¹æ¯çº¿ç¨åæ¥çæ¹å¼
å°±æ¯ä¸´çåºäºã临çåºåäºæ¥ç±»ä¼¼ãå®ä»¬ä¹é´çåºå«æ¯ï¼ä¸´çåºé度快ï¼ä½æ¯å®åªè½ç¨æ¥åæ¥åä¸ä¸ªè¿ç¨å
çå¤ä¸ªçº¿ç¨ã临çåºçè·ååéæ¾å½æ°å¦ä¸ï¼
EnterCriticalSection() è¿å
¥ä¸´çåº; LeaveCriticalSection()离å¼ä¸´çåºã 对äºå¤çº¿ç¨å
±äº«å
åçä¸ä¸å°±è®²å°è¿éäºã
(2)éç¨æ¶æ¯æºå¶è¿è¡å¤çº¿ç¨éä¿¡ååæ¥ï¼windowsä¸é¢ççæ¶æ¯æºå¶çå½æ°ç¨çå¤çå°±æ¯postmessageäºãLinuxä¸çæ¶æ¯æºå¶ï¼æç¨çè¾å°ï¼å°±ä¸å¨è¿é说äºï¼å¦æè°çæçï¼ä¹åè¯æï¼åµåµã
(3)windowsä¸çå¦å¤ä¸ç§çº¿ç¨éä¿¡æ¹æ³å°±æ¯äºä»¶åä¿¡å·éäºãåæ ·é对æå¼å§ä¸¾å¾ä¾åï¼2个线ç¨åæ¥ï¼ä»ä»¬ä¹é´ä¼ éä¿¡æ¯ï¼å¯ä»¥éç¨äºä»¶(Event)æä¿¡å·é(Semaphore),æ¯å¦ç¬¬ä¸ä¸ªçº¿ç¨å®æç产çæ°æ®åï¼å°±å¿
é¡»åè¯ç¬¬2个线ç¨ï¼ä»å·²ç»ææ°æ®åå¤å¥½äºï¼ä½ å¯ä»¥æ¥åèµ°äºã第2个线ç¨å°±ææ°æ®åèµ°ãåµåµï¼è¿éå¯ä»¥éç¨æ¶æ¯æºå¶ï¼å½ç¬¬ä¸ä¸ªçº¿ç¨åå¤å¥½æ°æ®åï¼å°±ç´æ¥postmessageç»ç¬¬2个线ç¨ï¼æç说éç¨postmessageä¸ä¸ªçº¿ç¨å°±å¯ä»¥æå®è¿ä¸ªé®é¢äºãåµåµï¼ä¸æ¯éç¹ï¼çç¥ä¸è®²äºã
对äºlinuxï¼ä¹æ类似çæ¹æ³ï¼å°±æ¯æ¡ä»¶åéäºï¼åµåµï¼è¿éwindowsålinuxå°±æä¸åäºãè¦ç¹å«è®²è®²æè¡ã
对äºwindowsï¼éç¨äºä»¶åä¿¡å·éåæ¥æ¶åï¼é½ä¼ä½¿ç¨waitforsingleobjectè¿è¡çå¾
çï¼è¿ä¸ªå½æ°ç第ä¸ä¸ªåæ°æ¯ä¸ä¸ªå¥æï¼å¨è¿éå¯ä»¥æ¯Eventå¥æï¼æSemaphoreå¥æï¼ç¬¬2个åæ°å°±æ¯çå¾
ç延è¿ï¼æç»çå¤ä¹
ï¼åä½æ¯msï¼å¦æè¿ä¸ªåæ°ä¸ºINFINITEï¼é£ä¹å°±æ¯æ éçå¾
äºãéæ¾ä¿¡å·éçå½æ°ä¸ºReleaseSemaphoreï¼ï¼ï¼éæ¾äºä»¶çå½æ°ä¸ºSetEventãå½ç¶ä½¿ç¨è¿äºä¸è¥¿é½è¦åå§åçãè¿éå°±ä¸è®²äºãMsdnä¸æï¼ç¥é©¬é½åºæ¥äºï¼åµåµãç¥é©¬é½æ¯æµ®äºï¼
对äºlinuxæä½ç³»ç»ï¼æ¯éç¨æ¡ä»¶åéæ¥å®ç°ç±»ä¼¼çåè½çãLinuxçæ¡ä»¶åéä¸è¬é½æ¯åäºæ¥éä¸èµ·ä½¿ç¨çï¼ä¸»è¦çå½æ°æï¼
pthread_mutex_lock ,
pthread_mutex_unlock,
pthread_cond_init
pthread_cond_signal
pthread_cond_wait
pthread_cond_timewait
为äºåwindowsæä½ç³»ç»è¿è¡å¯¹æ¯ï¼æç¨ä»¥ä¸è¡¨æ ¼è¿è¡æ¯è¾ï¼
对ç
§ä»¥ä¸è¡¨æ ¼,æ»ç»å¦ä¸ï¼
(1) Pthread_cleanup_push,Pthread_cleanup_popï¼
è¿ä¸å¯¹å½æ°pushåpopçä½ç¨æ¯å½åºç°å¼å¸¸éåºæ¶ï¼åä¸äºæ¸
é¤æä½ï¼å³å½å¨pushåpopå½æ°ä¹é´å¼å¸¸éåºï¼å
æ¬è°ç¨pthread_exitéåºï¼é½ä¼æ§è¡pushéé¢çæ¸
é¤å½æ°ï¼å¦ææå¤ä¸ªpushï¼æ³¨ææ¯æ¯æ ï¼å
æ§è¡åé¢çé£ä¸ªå½æ°ï¼å¨æ§è¡åé¢çå½æ°ï¼ä½æ¯æ³¨æå½å¨è¿2个å½æ°ä¹é´éè¿return éåºçè¯ï¼æ§ä¸æ§è¡pushåçå½æ°å°±çpopå½æ°ä¸çåæ°æ¯ä¸æ¯ä¸º0äºãè¿æå½æ²¡æå¼å¸¸éåºæ¶ï¼çåäºå¨è¿éé¢returnéåºçæ
åµï¼å³ï¼å½popå½æ°åæ°ä¸ä¸º0æ¶ï¼æ§è¡æ¸
é¤æä½ï¼å½popå½æ°åæ°ä¸º0æ¶ï¼ä¸æ§è¡pushå½æ°ä¸çæ¸
é¤å½æ°ã
ï¼2ï¼linuxçpthread_cond_signalåSetEventçä¸åç¹
Pthread_cond_singaléæ¾ä¿¡å·åï¼å½æ²¡æPthread_cond_waitï¼ä¿¡å·é©¬ä¸å¤ä½äºï¼è¿ç¹åSetEventä¸åï¼SetEventæ¯ä¸ä¼å¤ä½çã详解å¦ä¸ï¼
æ¡ä»¶åéçç½®ä½åå¤ä½æ2ç§å¸¸ç¨æ¨¡åï¼ç¬¬ä¸ç§æ¨¡åæ¯å½æ¡ä»¶åéç½®ä½æ¶(signaled)以åï¼å¦æå½å没æ线ç¨å¨çå¾
ï¼å
¶ç¶æä¼ä¿æ为置ä½(signaled)ï¼ç´å°æçå¾
ç线ç¨è¿å
¥è¢«è§¦å,å
¶ç¶ææä¼å为unsignaled,è¿ç§æ¨¡å以éç¨Windowså¹³å°ä¸çAuto-set Event 为代表ã
第2ç§æ¨¡ååæ¯Linuxå¹³å°çpthreadæéç¨ç模åï¼å½æ¡ä»¶åéç½®ä½(signaled)以åï¼å³ä½¿å½å没æä»»ä½çº¿ç¨å¨çå¾
ï¼å
¶ç¶æä¹ä¼æ¢å¤ä¸ºå¤ä½(unsignaled)ç¶æã
æ¡ä»¶åéå¨Linuxå¹³å°ä¸çè¿ç§æ¨¡åå¾é¾è¯´å¥½åï¼å¨å®é
åºç¨ä¸ï¼æ们å¯ä»¥å¯¹
代ç ç¨å æ¹è¿å°±å¯ä»¥é¿å
è¿ç§å·®å¼çåçãç±äºè¿ç§å·®å¼åªä¼åçå¨è§¦å没æ被线ç¨çå¾
å¨æ¡ä»¶åéçæ¶å»ï¼å æ¤æ们åªéè¦ææ¡å¥½è§¦åçæ¶æºå³å¯ãæç®åçåæ³æ¯å¢å ä¸ä¸ªè®¡æ°å¨è®°å½çå¾
线ç¨ç个æ°ï¼å¨å³å®è§¦åæ¡ä»¶åéåæ£æ¥è¯¥åéå³å¯ã
ç¤ºä¾ ä½¿ç¨ pthread_cond_wait() å pthread_cond_signal()
pthread_mutex_t count_lock;
pthread_cond_t count_nonzero;
unsigned count;
decrement_count()
{
pthread_mutex_lock(&count_lock);
while (count == 0)
pthread_cond_wait(&count_nonzero, &count_lock);
count = count - 1;
pthread_mutex_unlock(&count_lock);
}
increment_count()
{
pthread_mutex_lock(&count_lock);
if (count == 0)
pthread_cond_signal(&count_nonzero);
count = count + 1;
pthread_mutex_unlock(&count_lock);
}
(3) 注æPthread_cond_waitæ¡ä»¶è¿åæ¶äºæ¥éç解éé®é¢
extern int pthread_cond_wait __P ((pthread_cond_t *__cond,pthread_mutex_t *__mutex));
ãã è°ç¨è¿ä¸ªå½æ°æ¶,线ç¨è§£å¼mutexæåçé并被æ¡ä»¶åécondé»å¡ã线ç¨å¯ä»¥è¢«å½æ°pthread_cond_signalåå½æ° pthread_cond_broadcastå¤é线ç¨è¢«å¤éåï¼å®å°éæ°æ£æ¥å¤ææ¡ä»¶æ¯å¦æ»¡è¶³ï¼å¦æè¿ä¸æ»¡è¶³ï¼ä¸è¬è¯´æ¥çº¿ç¨åºè¯¥ä»é»å¡å¨è¿éï¼è¢«çå¾
被ä¸ä¸æ¬¡å¤éãå¦æå¨å¤çº¿ç¨ä¸éç¨pthread_cond_waitæ¥çå¾
æ¶ï¼ä¼é¦å
éæ¾äºæ¥éï¼å½çå¾
çä¿¡å·å°æ¥æ¶ï¼å次è·å¾äºæ¥éï¼å æ¤å¨ä¹åè¦æ³¨ææå¨è§£éã举ä¾å¦ä¸ï¼
#include
#include
#include
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /*åå§åäºæ¥é*/
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; //åå§åæ¡ä»¶åé
void *thread1(void *);
void *thread2(void *);
int i=1;
int main(void)
{
pthread_t t_a;
pthread_t t_b;
pthread_create(&t_a,NULL,thread1,(void *)NULL);/*å建è¿ç¨t_a*/
pthread_create(&t_b,NULL,thread2,(void *)NULL); /*å建è¿ç¨t_b*/
pthread_join(t_b, NULL);/*çå¾
è¿ç¨t_bç»æ*/
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
exit(0);
}
void *thread1(void *junk)
{
for(i=1;i<=9;i++)
{
printf("IN one\n");
pthread_mutex_lock(&mutex);//
if(i%3==0)
pthread_cond_signal(&cond);/*ï¼åéä¿¡å·ï¼éç¥t_bè¿ç¨*/
else
printf("thead1:%d\n",i);
pthread_mutex_unlock(&mutex);//*解éäºæ¥é*/
printf("Up Mutex\n");
sleep(3);
}
}
void *thread2(void *junk)
{
while(i<9)
{
printf("IN two \n");
pthread_mutex_lock(&mutex);
if(i%3!=0)
pthread_cond_wait(&cond,&mutex);/*çå¾
*/
printf("thread2:%d\n",i);
pthread_mutex_unlock(&mutex);
printf("Down Mutex\n");
sleep(3);
}
}
è¾åºå¦ä¸ï¼
IN one
thead1:1
Up Mutex
IN two
IN one
thead1:2
Up Mutex
IN one
thread2:3
Down Mutex
Up Mutex
IN one
thead1:4
Up Mutex
IN two
IN one
thead1:5
Up Mutex
IN one
Up Mutex
thread2:6
Down Mutex
IN two
thread2:6
Down Mutex
IN one
thead1:7
Up Mutex
IN one
thead1:8
Up Mutex
IN two
IN one
Up Mutex
thread2:9
Down Mutex
注æèè²çå°æ¹ï¼æ2个thread2:6ï¼å
¶å®å½è¿ä¸ªç¨åºå¤æ§è¡å 次ï¼i=3åi=6æ¶æå¯è½å¤æå°å 个ï¼è¿éå°±æ¯ç«äºéé æçäºã
(4)å¦å¤è¦æ³¨æçPthread_cond_timedwaitçå¾
çæ¯ç»å¯¹æ¶é´ï¼è¿ä¸ªåWaitForSingleObjectæ¯ä¸åçï¼Pthread_cond_timedwaitå¨ç½ä¸ä¹æ讨论ãå¦ä¸ï¼è¿ä¸ªé®é¢æ¯è¾ç»å
¸ï¼ææå®æ¬è¿æ¥ã
thread_a ï¼
pthread_mutex_lock(&mutex);
//do something
pthread_mutex_unlock(&mutex)
thread_bï¼
pthread_mutex_lock(&mutex);
//do something
pthread_cond_timedwait(&cond, &mutex, &tm);
pthread_mutex_unlock(&mutex)
æå¦ä¸ä¸¤ä¸ªçº¿ç¨thread_a, thread_b,ç°å¨å¦æaå·²ç»è¿å
¥äºä¸´çåºï¼èbåæ¶è¶
æ¶äºï¼é£ä¹bä¼ä»pthread_cond_timedwaitè¿ååï¼å¦æè½è¿åï¼é£å²ä¸æ¯a,bé½å¨ä¸´çåºï¼å¦æä¸è½è¿åï¼é£pthread_cond_timedwaitçå®æ¶å²ä¸æ¯å°±ä¸åäºï¼
大家讨论æä»·å¼ç2ç¹å¦ä¸ï¼
ï¼1ï¼ pthread_cond_timedwait (pthread_cond_t *cv, pthread_mutex_t *external_mutex, const struct timespec *abstime) -- This function is a time-based variant of pthread_cond_wait. It waits up to abstime amount of time for cv to be notified. If abstime elapses before cv is notified, the function returns back to the caller with an ETIME result, signifying that a timeout has occurred. Even in the case of timeouts, the external_mutex will be locked when pthread_cond_timedwait returns.
ï¼2ï¼ 2.1 pthread_cond_timedwaitè¡ä¸ºåpthread_cond_waitä¸æ ·ï¼å¨è¿åçæ¶åé½è¦å次lock mutex.
2 .2pthread_cond_timedwaitæè°çå¦æ没æçå°æ¡ä»¶åéï¼è¶
æ¶å°±è¿åï¼å¹¶ä¸ç¡®åã
å¦æpthread_cond_timedwaitè¶
æ¶å°äºï¼ä½æ¯è¿ä¸ªæ¶åä¸è½lock临çåºï¼pthread_cond_timedwait并ä¸ä¼ç«å³è¿åï¼ä½æ¯å¨pthread_cond_timedwaitè¿åçæ¶åï¼å®ä»å¨ä¸´çåºä¸ï¼ä¸æ¤æ¶è¿åå¼ä¸ºETIMEDOUTã
å
³äºpthread_cond_timedwaitè¶
æ¶è¿åçé®é¢ï¼æä¹è®¤åè§ç¹2ã
éå½ï¼
ãint pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict_attr,void*ï¼*start_rtn)(void*),void *restrict arg);
ããè¿åå¼ï¼è¥æååè¿å0ï¼å¦åè¿ååºéç¼å·
ããè¿åæåæ¶ï¼ç±tidpæåçå
ååå
被设置为æ°å建线ç¨ç线ç¨IDãattråæ°ç¨äºå¶å®åç§ä¸åç线ç¨å±æ§ãæ°å建ç线ç¨ä»start_rtnå½æ°çå°åå¼å§è¿è¡ï¼è¯¥å½æ°åªæä¸ä¸ªæ æéåæ°argï¼å¦æéè¦åstart_rtnå½æ°ä¼ éçåæ°ä¸æ¢ä¸ä¸ªï¼é£ä¹éè¦æè¿äºåæ°æ¾å°ä¸ä¸ªç»æä¸ï¼ç¶åæè¿ä¸ªç»æçå°åä½ä¸ºargçåæ°ä¼ å
¥ã
linuxä¸ç¨Cå¼åå¤çº¿ç¨ç¨åºï¼Linuxç³»ç»ä¸çå¤çº¿ç¨éµå¾ªPOSIX线ç¨æ¥å£ï¼ç§°ä¸ºpthreadã
ç± restrict 修饰çæéæ¯æåå¯ä¸å¯¹æéææåç对象è¿è¡ååçæ¹æ³ï¼ä»
å½ç¬¬äºä¸ªæéåºäºç¬¬ä¸ä¸ªæ¶ï¼æè½å¯¹å¯¹è±¡è¿è¡ååã对对象çååé½éå®äºåºäºç± restrict 修饰çæé表达å¼ä¸ã ç± restrict 修饰çæé主è¦ç¨äºå½æ°å½¢åï¼ææåç± malloc() åé
çå
å空é´ãrestrict æ°æ®ç±»åä¸æ¹åç¨åºçè¯ä¹ã ç¼è¯å¨è½éè¿ä½åº restrict 修饰çæéæ¯åå对象çå¯ä¸æ¹æ³çå设ï¼æ´å¥½å°ä¼åæäºç±»åçä¾ç¨ã
ãã第ä¸ä¸ªåæ°ä¸ºæå线ç¨æ è¯ç¬¦çæéã
ãã第äºä¸ªåæ°ç¨æ¥è®¾ç½®çº¿ç¨å±æ§ã
ãã第ä¸ä¸ªåæ°æ¯çº¿ç¨è¿è¡å½æ°çèµ·å§å°åã
ãã第å个åæ°æ¯è¿è¡å½æ°çåæ°ã
å 为pthreadä¸æ¯linuxç³»ç»çåºï¼æ以å¨ç¼è¯æ¶æ³¨æå ä¸-lpthreadåæ°ï¼ä»¥è°ç¨éæé¾æ¥åºã
ç»æ¢çº¿ç¨ï¼
å¦æå¨è¿ç¨ä¸ä»»ä½ä¸ä¸ªçº¿ç¨ä¸è°ç¨exitæ_exit,é£ä¹æ´ä¸ªè¿è¡ä¼ç»æ¢ï¼çº¿ç¨æ£å¸¸çéåºæ¹å¼æï¼
ï¼1ï¼ çº¿ç¨ä»å¯å¨ä¾ç¨ä¸è¿å(return)
ï¼2ï¼ çº¿ç¨å¯ä»¥è¢«å¦ä¸ä¸ªè¿ç¨ç»æ¢ï¼killï¼ï¼
ï¼3ï¼ çº¿ç¨èªå·±è°ç¨pthread_exitå½æ°
#include
pthread_exit
线ç¨çå¾
ï¼
int pthread_join(pthread_t tid,void **rval_ptr)
å½æ°pthread_joinç¨æ¥çå¾
ä¸ä¸ªçº¿ç¨çç»æãå½æ°åå为ï¼
ããextern int pthread_join __P (pthread_t __th, void **__thread_return);
第ä¸ä¸ªåæ°ä¸ºè¢«çå¾
ç线ç¨æ è¯ç¬¦ï¼ç¬¬äºä¸ªåæ°ä¸ºä¸ä¸ªç¨æ·å®ä¹çæéï¼å®å¯ä»¥ç¨æ¥åå¨è¢«çå¾
线ç¨çè¿åå¼ãè¿ä¸ªå½æ°æ¯ä¸ä¸ªçº¿ç¨é»å¡çå½æ°ï¼è°ç¨å®çå½æ°å°ä¸ç´çå¾
å°è¢«çå¾
ç线ç¨ç»æ为æ¢ï¼å½å½æ°è¿åæ¶ï¼è¢«çå¾
线ç¨çèµæºè¢«æ¶åã
对äºwindows线ç¨çå建ä¸è¥¿ï¼å°±ä¸å举äºï¼msdnä¸ ä¸æå°±åºæ¥äºãåµåµãä»å¤©å°±è®²å°è¿éå§ï¼å¸ææ¯æç å¼çï¼å¤§å®¶ä¸èµ·æ¢è®¨ï¼åµåµãé¨åå
容æä¹æ¯åèinternetçï¼ç¹æ¤å¯¹åä½è
表示æè°¢ï¼
温馨提示:答案为网友推荐,仅供参考