c++ boost线程glibc检测到无效指针

标签 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]

我不确定为什么会出现这种偶尔的行为。感谢任何帮助了解到底发生了什么。

感谢您的回复,

最佳答案

查看 thread_group header 告诉我以下内容。 boost::thread_group 期望您将指向分配有 new 的对象的指针传递给 add_thread 函数,因为它会尝试在析构函数中删除它们。

boost 文档列出了 add_thread 的以下预编码方法:

The expression delete thrd is well-formed and will not result in undefined behaviour.

其中 thrd 是 add_thread 方法的参数。

关于c++ boost线程glibc检测到无效指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11839417/

相关文章:

c++ - 标准化 2D/3D vector/坐标类

c++ - 如何在 C++ 中创建多个全局变量实例

multithreading - 如何在UML序列图中建模并行线程

c++ - 如何正确链接 boost::mpl::inherit_linearly 和 boost::mpl::inherit 以便解析占位符?

c++ - Boost Bimap 在调试版本中占用太多内存

C++ 什么时候编译类目标文件?

c++ - 循环遍历 Char 数组 - 拆分成单独的字符串 - Arduino

c++ - 是否明确定义了哪个线程调用thread_local存储的构造函数?

java - 如何使用多线程将多个文本文件读入一个列表?

c++ - boost::spirit ,如何获取占位符的 "value"