我有一個程序,我使用boost :: threads進行多線程。不幸的是,drd(valgrind --tool=drd ./my_program
)報告了大量10000問題。boost :: threads程序會導致很多競爭條件
我不確定是否誤解了boost線程的某些內容。我試圖找出我的錯誤幾個小時,但沒有得到更多,因此任何幫助將不勝感激。
我試圖管道某些過濾器,並希望能夠通過運行調用最後一個過濾器來運行它們。這個過濾器應該先調用他所依賴的所有前體過濾器,最後再調用他的processQueue()方法。 我現在想要能夠在他們的獲勝線程中調用前體過濾器,以便我可以加速如果圖形爲並行路徑。因此我添加了線程組,以便每個前體過濾器都在他自己的線程中執行。但不幸的是,我得到了很多競爭條件,我不確定他們的結果。 我希望現在更清楚我想達到什麼。
更新
我的代碼更新到哪裏的問題仍然出現,甚至簡單的代碼。我認爲這個問題與線程生成有關。
更新2
我想這些的主要原因是Valgrind的非常高的假陽性率。我已經開了一個關於這個問題的新問題。 See here
更新3
當我使用的Valgrind的3.6.1,而不是3.7.0或3.8.0大部分錯誤是可以避免的。 DRD的
這裏一個報告:
==29905== Conflicting load by thread 1 at 0xb0081000 size 8
==29905== at 0x25A6C2: pthread_join (in /usr/lib/system/libsystem_c.dylib)
==29905== by 0x2BEC0: boost::thread::join() (in /usr/local/lib/libboost_thread.dylib)
==29905== by 0x100006641: Filter::run() (in ./playgroudThreads)
==29905== by 0x100001013: main (in ./playgroudThreads)
==29905== Allocation context: unknown.
==29905== Other segment start (thread 2)
==29905== at 0x2A7B68: thread_start (in /usr/lib/system/libsystem_c.dylib)
==29905== Other segment end (thread 2)
==29905== at 0x3E667A: mach_msg_trap (in /usr/lib/system/libsystem_kernel.dylib)
==29905== by 0x3DED38: semaphore_create (in /usr/lib/system/libsystem_kernel.dylib)
==29905== by 0x2A50F7: new_sem_from_pool (in /usr/lib/system/libsystem_c.dylib)
==29905== by 0x2A6199: _pthread_exit (in /usr/lib/system/libsystem_c.dylib)
==29905== by 0x2A48C9: _pthread_start (in /usr/lib/system/libsystem_c.dylib)
==29905== by 0x2A7B74: thread_start (in /usr/lib/system/libsystem_c.dylib)
在這裏,我的例子代碼:
#include <iostream>
#include <vector>
#include <sys/time.h>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
class Filter
{
public:
Filter(int n) :
n_(n), precursor_(0)
{
}
~Filter()
{
}
void connect(Filter& f)
{
precursor_ = &f;
}
void run()
{
if (!isCalculationDone_) {
if (precursor_) {
boost::thread thread(&Filter::run, precursor_);
thread.join();
}
this->processQueue(2);
isCalculationDone_ = true;
}
}
void processQueue(unsigned N)
{
//do some calculations
}
public:
int n_;
Filter* precursor_;
bool isCalculationDone_;
};
int main(int argc, char* argv[])
{
Filter* f1 = new Filter(1);
Filter* f2 = new Filter(2);
f2->connect(*f1);
f2->run();
std::cerr << "main: done" << std::endl;
delete f2;
delete f1;
return 0;
}
;
請問你可能會先說你想達到什麼目的?我個人覺得很難理解。 (但後來我不那麼聰明) –
你爲什麼使用動態分配?它不僅是不必要的,而且是在泄漏它。 – GManNickG
@AndersK。我已經添加了解釋 – tune2fs