0
votes

Like a good deal of other people, I too am currently working on a little wrapper class for boost::thread and facing the problem to write a simple sleep(int miliseconds) function that can be called from another thread. Just like boost::thread::sleep() used to do. Since that function is going to be discontinued soon, there are new options: boost::this_thread::sleep_for() and sleep_until(). But I don't see how I could manage to achieve the wanted behaviour with those. And I also couldn't find a solution yet, even though this seems to be an issue to lots of people.

So I have my ThreadWrapper class with a boost::thread working inside. What I want to do is this:

ThreadWrapper thread(func_foo);
Thread.sleep(100); //tells the underlying boost:thread of my Threadwrapper
                   //to sleep 100ms

Any suggestions? Thanks!

1
To be clear, you want to 'inject' a sleep() call into another thread? - Martin James
Yes, pretty much so. My Class ThreadWrapper contains a boost thread and the function sleep(int ms) which should tell the boost thread object to sleep for the specified amount of time. But actually, this is a general question: Assume you create a boost::thread m_BoostThread in your main function. How would you put it to sleep out of your main function if you're not supposed to use m_BoostThread.sleep() anymore? - DenverCoder21
What problem are you ACTUALLY trying to solve (I feel this is a XY question - you think X is the answer to problem Y, so you as how to do X). - Mats Petersson
int main() { boost::thread mThread(func_foo); mThread.sleep(boost::chrono::milliseconds(50)); } How can I achieve this, if I'm not supposed to use sleep() anymore? The new options sleep_for() and sleep_until() only allow a thread to put itself to sleep, in my example these would be called in the func_foo function. - DenverCoder21
AFAIK, it it has never been possible to do what you seem to want in any kind of safe fashion. It would require considerable OS support to take a another thread out of the running state, (ie, it's running on another core than the one requesting the sleep). Suspending threads in this manner tends to have unfortunate implications for the process as a whole, eg. when a thread holding a global memory-management mutex gets suspended. - Martin James

1 Answers

0
votes

Seems to me this is application specific... like where in the target thread should a sleep occur? Let's say you've got a thread awaiting socket data... it's effectively already sleeping... do you want it to sleep some more when data actually arrives? Or stop waiting for data? Or maybe you have a thread crunching numbers... you may want it to sleep after a block of work. So, it seems to me you could have a simple class method setToPause(int ms) which sets a member sleeper value, then your worker/target thread checks the value at opportune times and resets and sleeps if it's set (or sleeps then resets, depending on your requirements). Not sure if that solves your specific problem (or even what that is) but it has to be easier than trying to poke around with thread scheduling...