#include
#include
#include
#include
#include
//線程處理函數(shù)
void *threaddeal(void *arg)
{
int i = *(int *)(arg);
printf("這是第%d個(gè)線程\n",i);
}
//主程序
int main(void)
{
//線程id
pthread_t threadid;
int j;
//創(chuàng)建大量線程
int count = 20;//多次循環(huán)
for(j=0 ; j < count ; j++)
{
//線程參數(shù)
int * p = &(j);
//創(chuàng)建線程
int ret= pthread_create(&threadid, NULL, threaddeal, (void*)p);
if(ret)//創(chuàng)建失敗
{
printf("創(chuàng)建線程失敗:%d\n",ret);
}
else//創(chuàng)建成功
{
//分離線程回收線程的stack占用的內(nèi)存
pthread_detach(threadid);
}
}
return 0;
}
當(dāng)前題目:[Linux線程]線程分離
轉(zhuǎn)載來(lái)源:
http://weahome.cn/article/jghijj.html