第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

為什么運(yùn)行結(jié)果為下面的情況呢?求解釋

為什么運(yùn)行結(jié)果為下面的情況呢?求解釋

忽然笑 2023-03-03 14:10:38
代碼如下:#include<pthread.h>#include<stdio.h>#define MAX_THREADS 5void *myThread(void *arg){printf("Thread %d started\n", (unsigned int)arg);pthread_exit(arg);}int main(){int ret, i, status;pthread_t threadids[MAX_THREADS];for(i=0; i<MAX_THREADS; i++){ret=pthread_create(&threadids[i], NULL, myThread, (void*)i);if(ret!=0)printf("error creating thread %d\n", i);}for(i=0; i<MAX_THREADS; i++){ret=pthread_join(threadids[i], (void** )&status);if(ret!=0)printf("error joining thread %d\n", i);elseprintf("status=%d\n",status);}return 0;}為什么運(yùn)行結(jié)果為:Thread 3 startedThread 4 startedThread 2 startedThread 1 startedThread 0 startedstatus=0status=1status=2status=3status=4謝謝!
查看完整描述

2 回答

?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊

Linux系統(tǒng)pthread_join用于掛起當(dāng)前線程(調(diào)用pthread_join的線程),直到thread指定的線程終止運(yùn)行為止,當(dāng)前線程才繼續(xù)執(zhí)行。

案例代碼:


/*********************************************    Name:pthread_join.c**    用于Linux下多線程學(xué)習(xí)**    案例解釋線程的暫停和結(jié)束**    Author:admin**    Date:2015/8/11       **    Copyright (c) 2015,All Rights Reserved!**********************************************#include <pthread.h>#include <unistd.h>#include <stdio.h>void *thread(void *str){    int i;    //不調(diào)用pthread_join線程函數(shù)    for (i = 0; i < 10; ++i)    {        sleep(2);        printf"This in the thread : %d\n" , i );    }    return NULL;} int main(){    pthread_t pth;    int i;    int ret = pthread_create(&pth, NULL, thread, (void *)(i));    //調(diào)用pthread_join線程函數(shù)    pthread_join(pth, NULL);    for (i = 0; i < 10; ++i)    {        sleep(1);        printf"This in the main : %d\n" , i );    }         return 0;}

通過Linux下shell命令執(zhí)行上面的案例代碼:


[root@localhost src]# gcc pthread_join.c -lpthread[root@localhost src]# ./a.outThis in the main : 0This in the thread : 0This in the main : 1This in the main : 2This in the thread : 1This in the main : 3This in the main : 4This in the thread : 2This in the main : 5This in the main : 6This in the thread : 3This in the main : 7This in the main : 8This in the thread : 4This in the main : 9

子線程還沒有執(zhí)行完畢,main函數(shù)已經(jīng)退出,那么子線程也就退出了,“pthread_join(pth, NULL);”函數(shù)起作用。


[root@localhost src]# gcc pthread_join.c -lpthread[root@localhost src]# ./a.outThis in the thread : 0This in the thread : 1This in the thread : 2This in the thread : 3This in the thread : 4This in the thread : 5This in the thread : 6This in the thread : 7This in the thread : 8This in the thread : 9This in the main : 0This in the main : 1This in the main : 2This in the main : 3This in the main : 4This in the main : 5This in the main : 6This in the main : 7This in the main : 8This in the main : 9

這說明pthread_join函數(shù)的調(diào)用者在等待子線程退出后才繼續(xù)執(zhí)行。

 


查看完整回答
反對(duì) 回復(fù) 2023-03-06
?
呼啦一陣風(fēng)

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊

這是隨機(jī)情況,由系統(tǒng)調(diào)度決定,不是唯一的結(jié)果,你可以嘗試這樣改:ret=pthread_create(&threadids[i], NULL, myThread, (void*)i);
sleep(1);
這樣就是按順序創(chuàng)建線程


查看完整回答
反對(duì) 回復(fù) 2023-03-06
  • 2 回答
  • 0 關(guān)注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)