C++ 11
我试图做出vector的std::thread秒.以下三点的组合说我可以.
1.)根据http://en.cppreference.com/w/cpp/thread/thread/thread,
thread默认构造函数创建一个
不代表线程的线程对象.
2.)根据http://en.cppreference.com/w/cpp/thread/thread/operator%3D,thread的operator=
使用移动语义将[参数,即线程右值引用]的状态分配给[调用线程].
3.)根据 http://en.cppreference.com/w/cpp/container/vector/vector,仅将大小类型变量传递给向量构造函数
具有[指定数量]的值初始化(对于类的默认构造)T实例的容器.没有复制.
所以,我这样做了:
#include <iostream>
#include <thread>
#include <vector>
void foo()
{
std::cout << "Hello\n";
return;
}
int main()
{
std::vector<std::thread> vecThread(1);
vecThread.at(0) = std::thread(foo);
vecThread.at(0).join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这在VC11和g ++ 4.8.0(在线编译器)中按预期运行,如下所示:
控制台输出:
Hello
Run Code Online (Sandbox Code Playgroud)
然后我在clang 3.2中尝试了它,通过在同一个网页上切换编译器菜单,这给出了:
stderr:
pure virtual method called
terminate called without an active exception
Run Code Online (Sandbox Code Playgroud)
当一个代表线程的线程对象在join()编辑或detach()编辑之前超出范围时,程序将被强制终止.我有join()ed vecThread.at(0),所以剩下的唯一问题是临时线程 …
这是一个非常小的C++ 11 Thread API代码,我正在尝试编译
#include<iostream>
#include<thread>
using namespace std;
void threadFunction(void)
{
cout<<"hello from thread:";//<<this_thread::get_id()<<endl;
}
int main()
{
std::thread t(threadFunction);
t.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在编译它为
g ++ thread1.cpp -pthread -std = c ++ 11
我得到以下错误
名为
terminate的纯虚方法在没有活动异常的情况下被
调用
Aborted
有什么不对,请有人帮忙
请注意我正在使用ARM A8处理器在Beaglebone Black上进行编译