![](/img/trans.png)
[英]Is it safe to mix boost::thread with C++11 std::mutex?
[英]Porting C++11 std::thread to boost::thread compile issues
我正在嘗試使用boost :: thread將C ++ 11 std :: thread代碼移植到VC9(VS 2008)。 下面的“等效” C ++ 11代碼在msvc12上可以正常編譯:
#include <iostream>
#include <thread>
#include <vector>
#include <algorithm>
#include <cassert>
void thFun(int i)
{
std::cout << "Hello from thread " << i << " !\n";
}
int main()
{
std::vector<std::thread> workers;
for (int i = 0; i < 10; ++i)
{
auto th = std::thread(&thFun, i);
workers.push_back(std::move(th));
assert(!th.joinable());
}
std::cout << "Hello from main!\n";
std::for_each(workers.begin(), workers.end(), [](std::thread& th)
{
assert(th.joinable());
th.join();
});
return 0;
}
我想使用msvc9編譯器和Boost 1.55將代碼移植到C ++ 03。 如何解決以下編譯錯誤:
#include <iostream>
#include <boost/thread.hpp>
#include <cassert>
#include <boost/foreach.hpp>
#include <boost/container/vector.hpp>
void thFun(int i)
{
std::cout << "Hello from thread " << i << " !\n";
}
void foo(boost::thread& th)
{
assert(th.joinable());
th.join();
}
int main()
{
boost::container::vector<boost::thread> workers;
for (int i = 0; i < 10; ++i)
{
BOOST_AUTO(th, boost::thread(&thFun, i));
workers.push_back(boost::move(th));
assert(!th.joinable());
}
std::cout << "Hello from main!\n";
BOOST_FOREACH(boost::thread& t, workers)
{
foo(t);
}
return 0;
}
編譯錯誤是:
d:\program data\boost\boost_1_55_0\boost\preprocessor\iteration\detail\local.hpp(37): error C2770: invalid explicit template argument(s) for '::boost::move_detail::enable_if_c<boost::enable_move_utility_emulation<T>::value&&!boost::move_detail::is_rv<T>::value,const T&>::type boost::forward(const ::boost::move_detail::identity<T>::type &)'
d:\program data\boost\boost_1_55_0\boost\move\utility.hpp(78) : see declaration of 'boost::forward'
d:\program data\boost\boost_1_55_0\boost\container\vector.hpp(1797) : see reference to function template instantiation 'void boost::container::allocator_traits<Alloc>::construct<T,T>(Alloc &,T *,const P0 &)' being compiled
with
[
Alloc=std::allocator<boost::thread>,
T=boost::thread,
P0=boost::thread
]
d:\program data\boost\boost_1_55_0\boost\container\vector.hpp(1791) : while compiling class template member function 'void boost::container::vector<T>::priv_push_back(const T &)'
with
[
T=boost::thread
]
d:\work\boostthreadscratch\boostthreadscratch\boostthreadscratch.cpp(19) : see reference to class template instantiation 'boost::container::vector<T>' being compiled
with
[
T=boost::thread
]
將此放在開頭:
#define BOOST_THREAD_USES_MOVE
請參閱此處以供參考。 它啟用Boost.Move( boost::move
)為boost::thread
提供的仿真,該仿真默認在Boost.Thread版本2中被禁用(在您的情況下MSVC9可以使用)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.