我對線程有點新,所以你不得不原諒這個問題的天真。pthread_join是如何實現的?
pthread_join
是如何實現的?它是如何影響線程調度的?
我總是使用while循環實現圖片pthread_join
,只是導致調用線程退出,直到目標線程完成。像這樣(非常近似的僞代碼):
atomic bool done; thread_run { do_stuff(); done = true; } thread_join { while(!done) { thread_yield(); // basically, make the thread that calls "join" on // our thread yield until our thread completes } }
這是一個準確的描述,還是我大大簡化了過程?
乾杯!
他的代碼和真實代碼之間唯一真正的巨大差異是如果有備用核心,他的代碼會導致加入的線程燒掉100%的CPU。 – 2011-08-30 13:10:38