1

次のコードでブースト スレッド ライブラリを試しています。

#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 は、デストラクタでそれらを削除しようとするため、new で割り当てられたオブジェクトへのポインタを add_thread 関数に渡すことを期待しています。

ブーストのドキュメントには、add_threadメソッドの次の事前コーディングがリストされています。

式 delete thrd は整形式であり、未定義の動作にはなりません。

thrd は add_thread メソッドへのパラメーターです。

于 2012-08-07T05:36:14.950 に答える