c ++ boost thread glibc检测到无效指针

时间:2012-08-07 04:46:49

标签: c++ multithreading boost glibc

我正在使用以下代码尝试使用boost线程库。

#include<iostream>
#include<boost/thread.hpp>

class TestSource{

private:

void workerFunc(int x){
    int p = 0;
    for (int i = 0; i < 100000; ++i){
        p += 1;
    }
    std::cout<<"thread worker #"<<x<<std::endl;
}

public:

TestSource(){}

~TestSource(){}

void testFunc(){
    boost::thread_group tg;

    boost::thread t1(&TestSource::workerFunc,this,1);
    boost::thread t2(&TestSource::workerFunc,this,2);
    boost::thread t3(&TestSource::workerFunc,this,3);
    boost::thread t4(&TestSource::workerFunc,this,4);
    boost::thread t5(&TestSource::workerFunc,this,5);
    boost::thread t6(&TestSource::workerFunc,this,6);
    boost::thread t7(&TestSource::workerFunc,this,7);
    boost::thread t8(&TestSource::workerFunc,this,8);
    boost::thread t9(&TestSource::workerFunc,this,9);
    boost::thread t10(&TestSource::workerFunc,this,10);

    tg.add_thread(&t1);
    tg.add_thread(&t2);
    tg.add_thread(&t3);
    tg.add_thread(&t4);
    tg.add_thread(&t5);
    tg.add_thread(&t6);
    tg.add_thread(&t7);
    tg.add_thread(&t8);
    tg.add_thread(&t9);
    tg.add_thread(&t10);

    tg.join_all();
}

};



int main(){
TestSource ts;
ts.testFunc();
return 0;
}

当我运行此程序时,“通常”获取表单的输出

thread worker #10
thread worker #7
thread worker #8
thread worker #6
thread worker #4
thread worker #3
thread worker #1
thread worker #9
thread worker #5
thread worker #2

但是,“偶尔”我会收到表格的输出,

thread worker #7
thread worker #8
thread worker #4
thread worker #2
thread worker #thread worker #5
thread worker #10
1
thread worker #6
thread worker #3
thread worker #9
*** glibc detected *** /home/aditya/workspace/testProj/all: free(): invalid pointer:   0xbff0616c ***
======= Backtrace: =========
[0x80cdd8f]
[0x80a260f]
[0x804c466]
[0x804cc8b]
[0x804a1c3]
[0x80b6266]
[0x804a089]
======= Memory map: ========
08048000-0818c000 r-xp 00000000 08:04 14812395   /home/aditya/workspace/testProj/all
0818c000-0818e000 rw-p 00143000 08:04 14812395   /home/aditya/workspace/testProj/all
0818e000-08198000 rw-p 00000000 00:00 0 
0a08d000-0a0af000 rw-p 00000000 00:00 0          [heap]
b2200000-b2221000 rw-p 00000000 00:00 0 
b2221000-b2300000 ---p 00000000 00:00 0 
b2400000-b2421000 rw-p 00000000 00:00 0 
b2421000-b2500000 ---p 00000000 00:00 0 
b2600000-b2621000 rw-p 00000000 00:00 0 
b2621000-b2700000 ---p 00000000 00:00 0 
b2738000-b2739000 rw-p 00000000 00:00 0 
b2739000-b273a000 ---p 00000000 00:00 0 
b273a000-b2f3a000 rw-p 00000000 00:00 0 
b2f3a000-b2f3b000 ---p 00000000 00:00 0  
b2f3b000-b373b000 rw-p 00000000 00:00 0 
b373b000-b373c000 ---p 00000000 00:00 0 
b373c000-b3f3c000 rw-p 00000000 00:00 0 
b3f3c000-b3f3d000 ---p 00000000 00:00 0 
b3f3d000-b473d000 rw-p 00000000 00:00 0 
b7742000-b7743000 rw-p 00000000 00:00 0 
b7743000-b7744000 r-xp 00000000 00:00 0          [vdso]
bfee6000-bff07000 rw-p 00000000 00:00 0          [stack]

我不确定为什么这种偶然的行为表现出来。感谢任何帮助,以了解究竟发生了什么。

感谢您的回复,

1 个答案:

答案 0 :(得分:1)

看看thread_group标题告诉我下面的内容。 boost :: thread_group希望你将指向new_赋值的对象的指针传递给add_thread函数,因为它会尝试在析构函数中删除它们。

增强文档列出了add_thread方法的以下预定义:

  

表达式删除thrd格式正确,不会导致   未定义的行为。

其中thrd是add_thread方法的参数。