我試圖讓這個簡單的測驗在另一個執行緒中創建一個嵌套執行緒,但它一直失敗。嘗試洗掉這個嵌套函式,代碼本身就可以正常作業。
代碼示例/測驗:
#include <thread>
#include <iostream>
#include <vector>
void simple() { std::cout << "\nNested Thread"; }
void function(int number, int* sum, std::vector<std::thread>& thread_group) {
*sum = number;
// Starting this nested function simple() in a thread causes errors.
// The problem occurs even when there's only a single iteration of the loop.
// Without it the code works fine, even with 10000 iterations.
std::thread ct(simple);
thread_group.push_back(std::move(ct));
}
int main(){
// Container for storing threads
std::vector<std::thread> thread_group;
int sum = 0;
for (int n = 1; n <= 10; n ) {
std::thread t(function, n, &sum, std::ref(r), std::ref(thread_group));
// Add the newly created thread to the container
thread_group.push_back(std::move(t));
}
// Wait for all threads to finish
join_all(thread_group);
return 0;
}
話雖如此,我需要找到一種呼叫獨立函式的方法,該函式可以在不阻塞其余代碼執行的情況下運行。執行緒或其他多處理方式在我的程式中非常重要,因為每個函式都是遞回的,并且會使用不同的引數不斷呼叫自身的多個副本,直到滿足條件。
我不知道在 std::thread 庫中是否有可能。嘗試使用 Poco 和 boost,但遇到了其他問題,因此決定改用 std::thread。
我在 Windows 10 上使用 Visual Studio 2022。
uj5u.com熱心網友回復:
sum您正在同時讀取和寫入thread_group多個執行緒。一個簡單的解決方案是確保一次只有一個執行緒可以訪問它們,方法是使用 astd::mutex并在訪問這些變數時鎖定它。您還需要確保所有執行緒thread_group在被銷毀之前都已完成作業。我添加了一個計數器和一個std::condition_variable來處理它。
示例(使用 C 20std::jthread代替不必呼叫不存在的join_all函式):
#include <condition_variable>
#include <iostream>
#include <list> // using list instead of vector to avoid realloc
#include <mutex>
#include <thread>
#include <vector>
using container = std::list<std::jthread>;
void simple() { std::cout << "Nested Thread\n"; }
void function(int& counter, std::mutex& mtx,
std::condition_variable& cv, container& thread_group)
{
std::lock_guard lock(mtx);
thread_group.emplace_back(simple);
counter; // so that main thread can check that all is done
cv.notify_one(); // signal main that the work is done
}
int main(){
// Container for storing threads
container thread_group;
std::mutex mtx;
std::condition_variable cv;
int starting_threads = 10;
int counter = 0;
for (int n = 1; n <= starting_threads; n ) {
std::lock_guard lock(mtx); // lock while adding thread
// Add thread to the container
thread_group.emplace_back(function, std::ref(counter), std::ref(mtx),
std::ref(cv), std::ref(thread_group));
}
std::unique_lock lock(mtx);
// wait until all threads are done with thread_group
// before letting thread_group go out of scope and get
// destroyed
while(counter < starting_threads) cv.wait(lock);
std::cout << thread_group.size() << '\n';
// wait for all threads to finish (jthread's auto-join)
}
可能的輸出:
Nested Thread
Nested Thread
Nested Thread
Nested Thread
Nested Thread
Nested Thread
Nested Thread
Nested Thread
Nested Thread
20
Nested Thread
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/424589.html
