【发布时间】:2014-07-29 10:46:29
【问题描述】:
我正在尝试运行以下测试程序:
#include <thread>
#include <iostream>
using namespace std;
struct foo
{
void t1()
{
for(int i = 0; i < 5; ++i)
cout << "thread 1" << endl;
}
thread bar()
{
return thread(&foo::t1, this);
}
};
void t2()
{
for(int i = 0; i < 5; ++i)
cout << "main " << endl;
}
int main()
{
foo inst;
inst.bar();
thread x(t2);
return 0;
}
“线程 1”运行但应用程序在它应该运行线程“x”时终止 输出是:
/home/user/dev/libs/llvm-3.4.2/bin/clang++ -std=c++11 -Wall -Wextra -pthread main.cpp -o 'Application' ./'Application' 在没有活动异常的情况下终止调用线程 1 线程 1 线程 1 线程 1 线程 1 个品牌:* [all] 中止
目标是使用另一个函数中的对象实例同时运行 2 个线程。
【问题讨论】:
标签: c++ multithreading c++11