简体   繁体   English

线程构造函数初始化 C++

[英]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:我尝试过的其他尝试:

  • Using references instead of regular value parameters使用引用而不是常规值参数
  • Using std::bind使用std::bind
  • Using std::function使用std::function
  • Declaring the function in a variable and passing that as my first parameter to the constructor在变量中声明 function 并将其作为我的第一个参数传递给构造函数
  • Compiling with different flags, like std=c++2a使用不同的标志编译,例如std=c++2a

EDIT :编辑

  1. I will leave the original issue as a means for others to learn from my mistakes.我会留下最初的问题,作为其他人从我的错误中吸取教训的一种手段。 As the answer I accept will show, this is due to my excessive usage of auto.正如我接受的答案将显示的那样,这是由于我过度使用 auto。 I had read a C++ book that basically said "always use auto, it's much more readable, Like Python and dynamic typing, but with the performance of C++."我读过一本 C++ 书,它基本上说“总是使用自动,它的可读性更高,就像 Python 和动态类型,但具有 ZF6F87C9FDCF8B3C3F07F93F1EE8712CZ9 的性能。” yet clearly this cannot always be done.但很明显,这并不总是可以做到的。 The 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM