pthread多線程編程 學(xué)習(xí)c 多線程編程主要用pthread還是c 11中的thread類?
學(xué)習(xí)c 多線程編程主要用pthread還是c 11中的thread類?主要使用pthread類1。創(chuàng)建一個(gè)線程類似于pthread的uuCreate函數(shù),前提是需要引入相應(yīng)的頭文件#include
學(xué)習(xí)c 多線程編程主要用pthread還是c 11中的thread類?
主要使用pthread類
1。創(chuàng)建一個(gè)線程
類似于pthread的uuCreate函數(shù),前提是需要引入相應(yīng)的頭文件
#include<pthread。H>
2。對(duì)于進(jìn)入線程的回調(diào)函數(shù),通常需要在回調(diào)函數(shù)中寫入死循環(huán)以進(jìn)行某些數(shù)據(jù)處理,等等
void t_u2;callback()]{
]while(true)
{]}
]3。由于線程將訪問相同的數(shù)據(jù)段或內(nèi)存,因此通常需要添加一個(gè)互鎖以防止訪問相同的數(shù)據(jù)段
pthreadmutexut mulock
pthreadmutexuuinit(&mulock,NULL)
pthreadmutexulock(&mulock)
pthreadmutexuunlock(&mulock);
4。死循環(huán)將導(dǎo)致崩潰,因此需要在死循環(huán)中添加一個(gè)塊,
pthreaduucondut mucond
pthreaduconduuinit(&mucond,null)
阻塞掛起時(shí)調(diào)用:
pthreaduconduwait(&mucond,&mucond)
阻塞喚醒時(shí)調(diào)用:
pthreaducondsingnal(&mucond)