c語言執行緒終止練習示例
程式碼如下:
#include
#include
#include
void *t1(void *args)
{
return (void *) 0;
}
void *t2(void *args)
{
printf("thread 2 param[args] = %dn", args);
pthread_exit((void *) 3);
}
void *t3(void *args) {
while(1)
{
printf("thread 3 is workingn");
sleep(1);
}
}
int main(int argc, char *argv[])
{
pthread_t thread;
int err;
void *status;
printf("creating thread 1n");
err = pthread_create(&thread, NULL, t1, NULL);
if(err)
{
printf("Can not created thread 1n");
exit(-1);
}
pthread_join(thread, &status);
printf("thread 1 exit return code %dnn", status);
printf("creating thread 2n");
err = pthread_create(&thread, NULL, t2, (void *) 9);
if(err)
{
printf("Can not created thread 2n");
exit(-2);
}
pthread_join(thread, &status);
printf("thread 2 exit return code %dnn", status);
printf("creating thread 3n");
err = pthread_create(&thread, NULL, t3, NULL);
if(err)
{
printf("Can not created thread 3n");
exit(-3);
}
sleep(10);
pthread_cancel(thread);
pthread_join(thread, &status);
printf("thread 3 exit return code %dn", status);
return 1;
}
-
Mac系統怎樣使用終端編寫c語言程式
C語言是目前較為普遍的程式語言,它功能豐富、表達能力強、使用靈活方便、目標程式效率高,適合編制一般應用軟體。下面是應屆畢業生小編收集整理的Mac系統怎樣使用終端編寫c語言程式,希望對大家有幫助~~Mac系統使用終端編寫c語言程式的'方法工具/原料Mac系統,終...
-
C語言中gets()函式知識
導語:gets從標準輸入裝置讀字串函式,其可以無限讀取,不會判斷上限,以回車結束讀取,所以程式設計師應該確保buffer的空間足夠大,以便在執行讀操作時不發生溢位。。下面是C語言中gets()函式知識,一起來學習下吧:gets()函式用於從緩衝區中讀取字串,其原型如下:char*gets(cha...
-
C語言猴子吃桃問題
猴子第一天摘了若干個桃子,當即吃了一半,還不解饞,又多吃了一個;第二天,吃剩下的桃子的一半,還不過癮,又多吃了一個;以後每天都吃前一天剩下的一半多一個,到第10天想再吃時,只剩下一個桃子了。問第一天共摘了多少個桃子?分析:1)設x1為前一天桃子數,設x2為第二天桃子數,則:x...
-
C語言考試機試經驗三部曲
全國計算機等級考試的持續升溫,至使全國計算機等級考試證書已成為國家公務員、國家企事業單位工作人員、公司員工等上崗、加薪、提拔的必備條件。以下是本站小編整理的C語言考試機試經驗三部曲,希望可以為您的考試帶來幫助!第一部:備考篇名將不打無準備之仗,複習備...