[英]C++ in vscode: error: no matching constructor for initialization of 'std::thread'
[英]no matching constructor for initialization of 'std::thread'
我一直在研究一種相當簡單的工具:並發for
循環構造,該構造采用輸入元素列表,輸出向量以及從輸入元素中計算輸出元素的函數。
我有一個無法編譯的代碼段:
template<class In, class Out>
void thread_do(net::coderodde::concurrent::queue<In>& input_queue,
Out (*process)(In in),
std::vector<Out>& output_vector)
{
// Pop the queue, process, and save result.
...
}
for (unsigned i = 0; i < thread_count; ++i)
{
thread_vector.push_back(std::thread(thread_do,
input_queue,
process,
output_vector));
}
我使用-std=c++14
。
./concurrent.h:129:45: error: no matching constructor for initialization of 'std::thread' thread_vector.push_back(std::thread(thread_do, ^ ~~~~~~~~~~
但是,我不知道如何解決它。 試圖在&
thread_do
/ thread_do
<In, Out>
前面加上&
,但沒有用。
這個最小的完整示例(提示)向您展示了如何在另一個線程中調用模板成員函數。
#include <thread>
struct X
{
template<class A, class B> void run(A a, B b)
{
}
template<class A, class B>
void run_with(A a, B b)
{
mythread = std::thread(&X::run<A, B>, this, a, b);
}
std::thread mythread;
};
int main()
{
X x;
x.run_with(10, 12);
x.mythread.join();
}
注意, std::thread
的構造函數無法自動推導模板參數。 您必須明確。
您需要實例化您的功能:
thread_vector.push_back(std::thread(thread_do<In, Out>, // you need to instantiate your template function
std::ref(input_queue), // pass parameters by ref
std::ref(process), // - // -
std::ref(output_vector))// - // -
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.