linux 平台c++ 11 库中中的多线程

jack_leiwin 2013-12-01 07:51:54
//testFunc函数

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();
}


运行至//thread 哪一行时,出现如下运行错误:
Program received signal SIGSEGV, Segmentation fault.
0x000000311340df1c in _dl_fixup () from /lib64/ld-linux-x86-64.so.2


/*****************************/
网上查了一下关于linux下,c++11 中的thread,几乎没有资料,都是里面pthread的东西,还请大牛指教一下,我在windows下运行上面的代码完全没有问题的。


...全文
418 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jack_leiwin 2013-12-01
  • 打赏
  • 举报
回复
引用 1 楼 ri_aje 的回复:
编译的时候加 -lpthread 了吗?
前辈不好意思,弄出来了,是没有加-lpthread,makefile内容太多了,有一个地方漏泄了!
jack_leiwin 2013-12-01
  • 打赏
  • 举报
回复
引用 6 楼 ri_aje 的回复:

#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!
前辈你好,其实我有一点忘记说了,是我的疏忽 我的那个while循环吧在一个类的成员函数里面了,实际代码下面

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,输出结果和你的一样,但是在我的程序里面,他就是提示段错误,郁了个闷!
ri_aje 2013-12-01
  • 打赏
  • 举报
回复

#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!
jack_leiwin 2013-12-01
  • 打赏
  • 举报
回复
引用 4 楼 ri_aje 的回复:
[quote=引用 3 楼 jack_leiwin 的回复:] [quote=引用 1 楼 ri_aje 的回复:] 编译的时候加 -lpthread 了吗?
必须的![/quote] 试了一下,g++-4.8 木问题。[/quote] “g++-4.8”还望大神你能够解释清楚一点...!不是很理解该怎么去做!
ri_aje 2013-12-01
  • 打赏
  • 举报
回复
引用 3 楼 jack_leiwin 的回复:
[quote=引用 1 楼 ri_aje 的回复:] 编译的时候加 -lpthread 了吗?
必须的![/quote] 试了一下,g++-4.8 木问题。
jack_leiwin 2013-12-01
  • 打赏
  • 举报
回复
引用 1 楼 ri_aje 的回复:
编译的时候加 -lpthread 了吗?
必须的!
ri_aje 2013-12-01
  • 打赏
  • 举报
回复
编译的时候加 -lpthread 了吗?

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧