Overview
Features
Download
Documentation
Community
Add-Ons & Services

Combining ThreadPool with Event

Please post support and help requests here.

Combining ThreadPool with Event

Postby ichijoji » 01 Oct 2009, 19:28

I'm trying to implement a kind of parallel for loop using a threadpool and events to indicate when a task is finished. My task looks something like this:
Code: Select all
class Task : public Poco::Runnable
{
    private:
        Poco::Event m_event;

    public:
        void run()
        {
            doSomeWork();
            m_event.set();
        }

        void wait()
        {
            m_event.wait();
        }
};


And then my loop looks like this: (Note that this is called recursively for each node in a tree of nodes)
Code: Select all
std::vector<Task*> tasks;

for ( some tasks )
{
    Task* task = new Task();
    threadPool.start(*task);
    tasks.push_back(task);
}

for ( std::vector<Task*>::iterator i = tasks.begin(); i != tasks.end(); ++i )
{
    (*i)->wait();
    delete *i;
}


This seems ok to me as an extremely naive implementation of a parallel for loop, but when I run it the program will chug along for a while, then hang. When it hangs, there are about 5 threads active, one waiting on the wait in Task::wait, and the others waiting with a stack trace like this:
Code: Select all
(gdb) bt
#0  0xb7ff8430 in __kernel_vsyscall ()
#1  0xb6f640e5 in pthread_cond_wait@@GLIBC_2.3.2 () from /lib/tls/i686/cmov/libpthread.so.0
#2  0xb707c2ed in pthread_cond_wait () from /lib/tls/i686/cmov/libc.so.6
#3  0xb7287a5b in Poco::EventImpl::waitImpl () from /usr/lib/libPocoFoundation.so.6
#4  0xb72ecb25 in Poco::PooledThread::run () from /usr/lib/libPocoFoundation.so.6
#5  0xb72e6f2f in Poco::ThreadImpl::runnableEntry () from /usr/lib/libPocoFoundation.so.6
#6  0xb6f604ff in start_thread () from /lib/tls/i686/cmov/libpthread.so.0
#7  0xb706d49e in clone () from /lib/tls/i686/cmov/libc.so.6


What I think is happening is that the capacity of the thread pool is filled up with threads waiting to create more threads in the thread pool. Is this a reasonable hypothesis, and if so how can it be tested and solved?
ichijoji
 
Posts: 2
Joined: 24 Jul 2009, 16:01

Return to Support

Who is online

Users browsing this forum: killerbobjr and 2 guests