[英]POSIX pthread using same thread multiple times
我現在所面對的是一個相當簡單的問題,我似乎無法解決。
我的程序中有兩個線程。 線程運行得很好,但是導致問題的原因是線程之一的重用。 以下為偽代碼:
main() {
create thread 1;
create thread 2;
join thread 1;
}
thread 1 {
while true
if(some condition)
join thread 2
// Use the returned value from thread 2
}
thread 2 {
while true
if(some condition)
// do something
exit thread 2(with some return value to thread 1).
}
因此,當線程1中滿足某些條件時,我希望它終止線程2,直到完成為止,這很好。 線程2達到條件並退出線程。 但是,當我回到線程1的while循環中並再次達到條件時,我希望它再次重新運行線程2。 這就是導致問題的原因。 執行一次線程2之后,線程1會忽略我的join語句,而只是在while循環中輪詢,這是唯一運行的線程。
所以我的問題是。 如何重用連接線程2屬性,以便程序連續運行?
執行一次線程2之后,線程1會忽略我的join語句,而只是在while循環中輪詢,這是唯一運行的線程。
請注意,在您的情況下,pthread_join()最有可能會失敗,並出現兩次錯誤。 檢查其返回值。
但是,由於線程2已退出,因此沒有等待的線程。 您必須再次啟動線程2。 那是:
thread 1 {
while true
if(some condition)
join thread 2
// Use the returned value from thread 2
create thread 2;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.