移植C++11 std::线程以提升::线程编译问题
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 databoostboost_1_55_0boostpreprocessoriterationdetaillocal.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 databoostboost_1_55_0boostmoveutility.hpp(78) : see declaration of 'boost::forward'
d:program databoostboost_1_55_0boostcontainervector.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 databoostboost_1_55_0boostcontainervector.hpp(1791) : while compiling class template member function 'void boost::container::vector<T>::priv_push_back(const T &)'
with
[
T=boost::thread
]
d:workboostthreadscratchboostthreadscratchboostthreadscratch.cpp(19) : see reference to class template instantiation 'boost::container::vector<T>' being compiled
with
[
T=boost::thread
]
把这个放在开头:
#define BOOST_THREAD_USES_MOVE
请参阅此处以供参考。它为boost::thread
启用 Boost.Move (boost::move
) 提供的仿真,默认情况下在 Boost.Thread 版本 2 中处于禁用状态(在您的情况下,MSVC9 可能会使用它)。
相关文章:
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 线程构造函数周围的可变参数模板包装器无法编译
- 使用提升线程时编译失败
- 编译问题 C++ 同时,尝试通过调用另一个对象中的成员函数来创建 std:: 线程
- C++ 提升示例:创建和管理线程(编译错误)
- 如何使用mingw编译C STD ::线程代码
- 为什么我不能编译这个简单的线程测试?
- 使用 -march 编译会导致线程说"pure virtual method called"
- 使用优化标志进行编译时线程不启动
- C++线程错误 - 带有类参数的编译时错误
- 使用优化编译时的多线程程序块
- 使用英特尔线程构建块编译:错误操作数为?:
- 移植C++11 std::线程以提升::线程编译问题
- C++ - 创建线程编译错误
- boost::线程编译错误
- 在android ndk中为线程编译c++代码时出现memset错误
- MinGw 4.7.0 不使用 #include < boost/线程编译代码>
- Boost可选的Boost线程编译问题
- 如何解决boost多线程编译错误的简单程序
- 使用QT5_ADD_RESOURCES和CMake进行多线程编译时,资源.cpp文件已损坏