我有一个程序,我使用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