#include
#include
#include
//線程1的啟動(dòng)函數(shù)
void *threaddeal1(void *arg)
{
printf("thread 1 returning\n");
return((void *)1);
}
//線程2的啟動(dòng)函數(shù)
void *threaddeal2(void *arg)
{
printf("thread 2 exiting\n");
pthread_exit((void *)2);
}
//主函數(shù)
int main(int argc,char *argv[])
{
int err;
pthread_t threadid1, threadid2;
void *tret;
//創(chuàng)建線程1
err = pthread_create(&threadid1, NULL, threaddeal1, NULL);
if (err != 0) //創(chuàng)建線程1失敗
{
printf("創(chuàng)建線程1失敗,錯(cuò)誤為: %s\n", strerror(err));
}
//創(chuàng)建線程2
err = pthread_create(&threadid2, NULL, threaddeal2, NULL);
if (err != 0)
{
printf("創(chuàng)建線程2失敗,錯(cuò)誤為: %s\n", strerror(err));
}
//阻塞線程1
err = pthread_join(threadid1, &tret);
if (err != 0)
{
printf("阻塞線程1失敗,錯(cuò)誤為: %s\n", strerror(err));
}
//退出并且打印線程1的退出狀態(tài)
printf("線程1的退出碼為 %d\n", (int)tret);
//阻塞線程2
err = pthread_join(threadid2, &tret);
if (err != 0)
{
printf("阻塞線程2失敗,錯(cuò)誤為: %s\n", strerror(err));
}
//退出并且打印線程2的退出狀態(tài)
printf("線程2的退出碼為 %d\n", (int)tret);
exit(0);
}
文章標(biāo)題:[Linux線程]多線程的阻塞和退出
標(biāo)題網(wǎng)址:
http://weahome.cn/article/jshihc.html