Boost logo

Boost Users :

Subject: [Boost-users] single thread in loop
From: SRD (software.research.development_at_[hidden])
Date: 2013-07-01 23:54:43


I'm using boost 1.53 on windows 7. I have a main loop which runs some
checks and when events are received, results in callbacks that add tasks
onto a queue. I then pull a task off the queue, wish to start a thread (and
only one thread) to handle the task, and continue to loop checking for
certain events. And each time I go through the loop, I want to check to see
if the current thread has completed before pulling off another task to
process.

My problem is that I'm not sure how to write the thread in to do this and
not join because I have to keep looping to check for certain events.

void my_callback(...);

MyTask *task;

while (true) {
        event_checks();

    switch(state) {
    case STATE1:
        if (!queue.empty())
            task = queue.front();
        // ...
        break;
    case STATE2:
        // check if thread is still running, otherwise, start new thread
        boost::thread(&MyTask::do_it, task);
        // ...
        break;

    // ...
}

I tried things similar to the below but couldn't get that quite right.

boost::thread t;

while (true) {
// ...
if (t.timed_join(0) == false) {
    t = boost::thread(&MyTask::do_it, task);
}
// ...

Any help much appreciated.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net