[英]Thread Constructor Initialization C++
I have been attempting to write a simple program to experiment with vectors of threads.我一直在尝试编写一个简单的程序来试验线程向量。 I am trying to create a thread at the moment, but I am finding that I am running into an error that my constructor is not initializing properly, with the error that there is no matching constructor for std::thread matching the argument list.
我目前正在尝试创建一个线程,但我发现我遇到了一个错误,即我的构造函数没有正确初始化,错误是没有匹配参数列表的 std::thread 构造函数。 Here is what I have done:
这是我所做的:
#include <functional>
#include <iostream>
#include <numeric>
#include <thread>
#include <vector>
int sum = 0;
void thread_sum (auto it, auto it2, auto init) {
sum = std::accumulate(it, it2, init);
}
int main() {
// * Non Multi-Threaded
// We're going to sum up a bunch of numbers.
std::vector<int> toBeSummed;
for (int i = 0; i < 30000; ++i) {
toBeSummed.push_back(1);
}
// Initialize a sum variable
long sum = std::accumulate(toBeSummed.begin(), toBeSummed.end(), 0);
std::cout << "The sum was " << sum << std::endl;
// * Multi Threaded
// Create threads
std::vector<std::thread> threads;
std::thread t1(&thread_sum, toBeSummed.begin(), toBeSummed.end(), 0);
std::thread t2(&thread_sum, toBeSummed.begin(), toBeSummed.end(), 0);
threads.push_back(std::move(t1));
threads.push_back(std::move(t2));
return 0;
}
The line that messes up is the following:搞砸的行如下:
auto t1 =
std::thread {std::accumulate, std::ref(toBeSummed.begin()),
It is an issue with the constructor.这是构造函数的问题。 I have tried different combinations of std::ref, std::function, and other wrappers, and tried making my own function lambda object as a wrapper for accumulate.
I have tried different combinations of std::ref, std::function, and other wrappers, and tried making my own function lambda object as a wrapper for accumulate.
Here is some additional information:以下是一些附加信息:
The error message is: atomics.cpp:28:7: error: no matching constructor for initialization of 'std::thread'错误消息是:atomics.cpp:28:7: 错误:没有匹配的构造函数用于初始化 'std::thread'
Moreover, when hovering over the constructor, it tells me that the first parameter is of <unknown_type>.此外,当悬停在构造函数上时,它告诉我第一个参数是 <unknown_type>。
Other attempts I have tried:我尝试过的其他尝试:
std::bind
std::bind
std::function
std::function
std=c++2a
std=c++2a
EDIT :编辑:
using
keyword provides the readability while still the safety. using
关键字提供了可读性,同时仍然安全。 Thank you for the answers! The problems you're encountering are because std::accumulate
is an overloaded function template, so the compiler doesn't know what specific function type to treat it as when passed as an argument to the thread constructor.您遇到的问题是因为
std::accumulate
是一个重载的 function 模板,所以编译器不知道什么特定的 function 类型将其视为作为参数传递给线程构造函数时。 Similar problems arise with your thread_sum
function because of the auto
parameters.由于
auto
参数,您的thread_sum
function 也会出现类似问题。
You can choose a specific overload/instantiation of std::accumulate
as follows:您可以选择
std::accumulate
的特定重载/实例化,如下所示:
std::thread t2(
(int(*)(decltype(toBeSummed.begin()), decltype(toBeSummed.end()), int))std::accumulate,
toBeSummed.begin(), toBeSummed.end(), 0);
The problem is your excessive use of auto
.问题是您过度使用
auto
。 You can fix it by changing this one line:您可以通过更改这一行来修复它:
void thread_sum (auto it, auto it2, auto init) {
To this:对此:
using Iter = std::vector<int>::const_iterator;
void thread_sum (Iter it, Iter it2, int init) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.