Boost logo

Boost Users :

Subject: Re: [Boost-users] problems with boost bind and boost thread
From: Marco (marco.schumann_at_[hidden])
Date: 2012-03-22 11:02:46


Hi,

thanks for your hints (also to Gabriel Redner)!

| I can't realise right now what type trait could be useful for this
| purpose...

I want co call a function or method in a separate thread periodically.
Therefore my CCycleThread class starts the execute method as thread and
calls the user function object in a loop. The user can pause, resume and
finish the thread if he wishes.
To separate the worker code from the threading code I build this wrapper
around boost thread with pause and resume methods.

class CCycleThread : boost::noncopyable
{

template<typename T>
CCycleThread(T t) ...

void pause() ...

void resume() ...

void finish() ...

...

template<typename T>
void execute(T t)
{

        boost::unique_lock<boost::mutex> lock(m_mut,boost::defer_lock_t());
        for(;;)
        {
                //check if the work should be paused or finished
                lock.lock();

                while(!m_doWork)
                {
                        //to do: inform user object about pause
                        m_cond.wait(lock);
                }
                //to do: inform user object about resume
                if (m_finished) break;

                lock.unlock();

                //do the work
                t();
        }
}
};

And if the user passes a bind object than I have the binding of the binding.


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