我有一個程序,不斷得到一些工作要做(像服務器的東西),並且線程少。哪種方法可以重用pthread庫中的線程?或者我每次都被迫創建一個新的線程。我想重複使用至少pthread_t
結構。我想這樣的事情:重用線程pthread
int main() {
pthread_t threads[some value];
while (1) {
get work;
find a free thread;
pthread_create(free thread, do work);
pthread_join(done threads);
}
但我不知道如何正確地釋放線程或如何檢查它是否是免費的。
您是否考慮了線程構建模塊(TBB)? – Adam