简体   繁体   English

boost :: thread和std :: thread之间的区别

[英]Difference between boost::thread and std::thread

I have a place where things work using boost::thread(example using boost::asio) 我有一个地方使用boost :: thread工作(使用boost :: asio的示例)

  std::vector<boost::shared_ptr<boost::thread> > threads;
  for (std::size_t i = 0; i < io_services_.size(); ++i)
  {
    boost::shared_ptr<boost::thread> thread(new boost::thread(
          boost::bind(&boost::asio::io_service::run, io_services_[i])));
    threads.push_back(thread);
  }

If I try to use it with std:thread I get compile error: 如果我尝试将其与std:thread一起使用,则会出现编译错误:

std::vector<std::thread> threads;
for (std::size_t i = 0; i < this->ioServices.size(); ++i)
{
    std::thread thread(&boost::asio::io_service::run, ioServices[i]); // compile error std::thread::thread : no overloaded function takes 2 arguments   

    threads.push_back(std::move(thread));
}

In theory, both should work, since std::thread has a vararg constructor which basically invokes its arguments as if it were used with std::bind . 从理论上讲,两者都应该起作用,因为std::thread具有vararg构造函数,该构造函数基本上调用其参数,就像将其与std::bind一起使用一样。 The problem appears to be that, at least in my implementation (gcc 4.6.3), neither std::thread nor std::bind can determine which overload of run was intended, resulting in a compilation error. 问题似乎是,至少在我的实现中(gcc 4.6.3), std::threadstd::bind都无法确定run预期重载,从而导致编译错误。

However, if you use boost::bind , this works. 但是,如果使用boost::bind ,则可以使用。 So I would use, and manually perform the bind manually: 因此,我将使用并手动执行绑定:

std::vector<std::thread> threads;
for (std::size_t i = 0; i < this->ioServices.size(); ++i)
{
    std::thread thread(boost::bind(&boost::asio::io_service::run, ioServices[i])); 

    threads.push_back(std::move(thread));
}

Edit: It appears that boost::bind succeeds because it's got a ton of overloads, and based on the number of arguments it was provided, during overload resolution and template substitution of boost::bind it can determine which overload of boost::asio::io_service::run was intended. 编辑:看来boost::bind成功,因为它有大量的重载,并且根据提供的参数数量,在重载解析和boost::bind模板替换期间,它可以确定boost::asio::io_service::run重载boost::asio::io_service::run是预期的。

However, since std::bind and std::thread rely on a vararg tempalte arguments, both overloads of run are equally valid, and the compiler cannot resolve which one to use. 但是,由于std::bindstd::thread依赖于vararg tempalte参数,因此run两个重载都同样有效,并且编译器无法解析使用哪一个。 This ambiguity results in a failure to determine which results in the failures you are seeing. 这种歧义会导致无法确定导致您看到的故障的原因。

So another solution is: 因此,另一个解决方案是:

std::vector<std::thread> threads;
typedef std::size_t (boost::asio::io_service::*signature_type)();
signature_type run_ptr = &boost::asio::io_service::run;

for (std::size_t i = 0; i < this->ioServices.size(); ++i)
{
    std::thread thread(run_ptr, ioServices[i]); 

    threads.push_back(std::move(thread));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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