我有一个程序,在其中我使用boost::threads进行多线程处理。不幸的是drd(valgrind --tool=drd ./my_program)报告了许多关于10000的问题。

我不确定是否误解了Boost线程。我试图找出我的错误数小时,但没有得到更多的帮助,因此,我们将不胜感激。

我尝试通过管道传递某些过滤器,并希望能够通过调用带有run的最后一个过滤器来运行它们。然后,此过滤器应首先调用其依赖的所有前驱过滤器,最后调用其processQueue()方法。
现在,我希望能够在它们的won线程中调用前体过滤器,以便将图形作为并行路径来加快速度。因此,我添加了线程组,以便每个前体过滤器都在自己的线程中执行。但是不幸的是,我在很多比赛条件下不确定它们的来源。
我希望现在可以更清楚地了解我要实现的目标。

更新

我已经将代码更新为一个甚至更简单的代码,在该代码中仍然会出现问题。我认为问题出在某种程度上与线程生成有关。

更新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;

}
;

最佳答案

您并不孤单:在这里看到thread,这表明问题是误报““可能是由于重新创建的线程将内存从终止线程中重新用于线程本地存储而引起的”。

10-01 05:40