[問題] 想請教一下有關pthread的關念。
最近在看 beginning linux programming 4th
有關pthread的地方不是很懂。想請大家指點一下小弟。
int main()
res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
if (res != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
while(print_count1++ < 20) {
// printf("i am main()\n");
if (run_now == 1) {
printf("1 ");
run_now = 2;
}
else {
sleep(1);
}
}
}
void *thread_function(void *arg) {
int print_count2 = 0;
while(print_count2++ < 20) {
// printf("i am thread()\n");
if (run_now == 2) {
printf("2 ");
run_now = 1;
}
else {
sleep(1);
}
}
}
我想請問的觀念是為什麼不是馬上1 2 1 2.....C 一直印出來? 而是等一段時間過後,1 2 1 2.....才一次全部印出來。
但是如果把printf加上去,就會馬上印出來,有人可以指點一下pthread的關念嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 202.5.224.98
推
02/25 16:24, , 1F
02/25 16:24, 1F
推
02/25 18:09, , 2F
02/25 18:09, 2F
推
02/25 20:49, , 3F
02/25 20:49, 3F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章