64,651
社区成员
发帖
与我相关
我的任务
分享
void testFunc(int a)
{
cout<<"a=:"<<a<<" this is the test function!"<<endl;
}
int k=3,p=2;
while(k>0)
{
k--;
thread th(testFunc,p);// thread
th.join();
}
void testFunc(int a)
{
cout<<"a=:"<<a<<" this is the test function!"<<endl;
}
void CIndex::parallelB(vector<shared_ptr<CObject> > &dataObjectList)
{
...
int k=3;
while(k>0)
{
k--;
thread th(testFunc,k);// thread
th.join();
}
...
}
我觉得这个在函数内函数外没有区别,但是我也试过前辈你那样直接在main中写while,输出结果和你的一样,但是在我的程序里面,他就是提示段错误,郁了个闷!
#include <iostream>
#include <thread>
using namespace std;
void testFunc(int a)
{
cout<<"a=:"<<a<<" this is the test function!"<<endl;
}
int main ()
{
int k=3;
while(k>0)
{
k--;
thread th(testFunc,k);// thread
th.join();
}
}
g++-4.8 -std=c++11 -O2 -Wall -lpthread main.cpp && ./a.out
试试。
我这里的输出。
a=:2 this is the test function!
a=:1 this is the test function!
a=:0 this is the test function!