1

我正在使用以下代码尝试 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]

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

感谢您的回复,

4

1 回答 1

1

查看 thread_group 标头告诉我以下内容。boost::thread_group 期望您将指针传递给 add_thread 函数,因为它将尝试在析构函数中删除它们。

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

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

其中 thrd 是 add_thread 方法的参数。

于 2012-08-07T05:36:14.950 回答