我讀了一本書,這給下一個代碼:的Posix /線程加入
void *printme(void *id) {
int *i;
i = (int *)id;
printf("Hi. I'm thread %d\n", *i);
return NULL;
}
void main() {
int i, vals[4];
pthread_t tids[4];
void *retval;
for (i = 0; i < 4; i++) {
vals[i] = i;
pthread_create(tids+i, NULL, printme, vals+i);
}
for (i = 0; i < 4; i++) {
printf("Trying to join with tid%d\n", i);
pthread_join(tids[i], &retval);
printf("Joined with tid%d\n", i);
}
}
和未來可能的輸出:
Trying to join with tid0
Hi. I'm thread 0
Hi. I'm thread 1
Hi. I'm thread 2
Hi. I'm thread 3
Joined with tid0
Trying to join with tid1
Joined with tid1
Trying to join with tid2
Joined with tid2
Trying to join with tid3
Joined with tid3
而且我不明白這怎麼可能。我們從主線程開始,創建4個線程:tids[0]... tids[3]
。然後,我們暫停執行(通過加入指令):主線程將等待tids[0]
停止執行,tids[0]
將等待到tids[1]
等等。
所以輸出應該是:
Hi. I'm thread 0
Hi. I'm thread 1
Hi. I'm thread 2
Hi. I'm thread 3
Trying to join with tid0
Trying to join with tid1
Joined with tid0
Trying to join with tid2
Joined with tid1
Trying to join with tid3
Joined with tid2
Joined with tid3
我覺得我不明白真正的基本的東西。謝謝。
線程有另一個執行空間,他們只是執行該函數,他們不會期望到達第二個循環,他們的執行將只是printme。他們無法處理任何訂單,所以在這種情況下,所有線程都會被創建,並且控制權仍在主線程中。當連接被調用時,它會進入睡眠狀態,任何線程都可以工作。它返回並釋放本身的連接。 – Mustafa