4

ブースト::スレッドを使用して物事が機能する場所があります(ブースト::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);
  }

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));
}
4

1 に答える 1

2

理論std::thread的には、基本的に で使用されているかのように引数を呼び出す vararg コンストラクターがあるため、両方が機能するはずstd::bindです。問題は、少なくとも私の実装 (gcc 4.6.3) では、どちらのオーバーロードが意図されているかを判断することstd::threadもできないため、コンパイル エラーが発生することです。std::bindrun

ただし、 を使用するboost::bindと、これは機能します。したがって、手動でバインドを使用し、手動で実行します。

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));
}

編集:boost::bind大量のオーバーロードがあり、提供された引数の数に基づいて、オーバーロードの解決とテンプレートの置換中に、どのオーバーロードが意図された boost::bindかを判断できるため、成功したようです。boost::asio::io_service::run

ただし、std::bindstd::threadは vararg tempalte 引数に依存しているため、 の両方のオーバーロードrunは等しく有効であり、コンパイラはどちらを使用するかを解決できません。このあいまいさにより、表示されているエラーの原因を特定できません。

したがって、別の解決策は次のとおりです。

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));
}
于 2012-11-20T15:46:49.413 に答える