views:

173

answers:

3

let's say i have a blocking method , let's call in Block().

as i don't want my main thread to block i might create a worker thread, that instead will call Block.

however, i have another condition.

i want the call to block to return in 5 seconds top, otherwise, i want to let the main thread know the call to Block failed and to exit the worker thread.

what would be the best solution to that scenario?

i thought something like that: create a workher thread, in the worker thread to create a timer object with 5 seconds, and in addition to call gettickcount before and after the call to Block and calculate the delta.

in addition i will define a boolean IsReturned indication whether the Block function returned already. after the Block call to set it true.

according to that boolean in the Timer Function i decide how to proceed :

  1. if the boolean is true i do nothing.

  2. if the boolean is false i can queue an APC OnFailure or maybe signal Sucess event on the main thread, and exit the worker thread forcfully (the thing is i'm not sure if i can do that)

in addition after the block function return i check whether the delta is lett then 5 sec and queue an APC OnSucess. (the question is does exiting the caller thread cancels the timer also ? cause basically after that the timer is useless )

p.s - if i can know for sure that i can cancel the worker thread within the timer function i don't think i even need the gettickcount stuff.

thanks!

+2  A: 

I think you have roughly the right idea, though you probably want the WM_TIMER messages delivered to the main thread, not the potentially-blocking thread. Otherwise, the timer messages might get lost, if the thread blocks before the timer fires! Similarly, check the elapsed time in the main thread rather than the worker, since if Block() blocks, it won't return, and the call to GetTickCount() after Block() will never happen.

To communicate between threads, the simplest thing would probably be to use an atomic variable. You could also have the worker pass a message back to the main thread on success, and if the main thread doesn't see the message when the 5-second timer fires, it should assume the worker thread blocked.

In general, killing a blocked thread can be dangerous. The Java documentation cautions strongly against doing it, and if anything, the problems are worse with C++. Consider yourself warned!

Ben Karel
A: 

First creating threads is an expensive thing so doing for each call to Block may not be a good idea.

Second there are numerous ways to solve this, it also depends heavily on your environment. For instance in Windows a possible way of doing this would be to have a worker thread with a message queue. You then define a couple of messages which you handle in your worker thread. One could be WM_CALLBLOCK another could be WM_AREYOUREADY and WM_YESIAM, when you want to call Block() you could then just post that message to the worker thread and it would call the function. With the message you could also pass whatever parameters you need for Block(). Since your function is blocking - if you then post a message WM_AREYOUREADY you will not get a WM_YESIAM reply directly. So you could build your timeout on that.

Anders K.
+2  A: 

I would suggest using the boost::threads library for this. You can periodically check if the blocking thread is joinable (ie, still working) and then interrupt it after five seconds. You will then need to write the blocking function to handle that interruption and cleanly exit.

#include <boost/thread/thread.hpp>

void Block(void)
{
    //Do work and periodically call boost::this_thread::sleep()
    try
    {
        boost::this_thread::sleep(boost::posix_time::milliseconds(100));
    }
    catch(boost::thread_interrupted const&)
    {
        return;
    }
}

int main(int argc, char *argv[])
{
    boost::thread blockThread(Block); //If Block takes arguments, just add them as arguments to the constructor.
    time_t startTime = time(NULL);

    while(true)
    {
        if(blockThread.joinable() && (time(NULL) - startTime) > 5)
        {
            blockThread.interrupt();
        }
        //Do whatever you want while waiting for the thread to finish.
    }
}

Edit: Check the Thread Management documentation for more interruption points and the definition for the boost threads class.

Edit2: If you don't need to do any work in the main thread while waiting for the blocking thread to complete, and there is no convenient place to handle interrupts in Block() you can explicitly kill the thread with something like this:

void Block(void)
{
    //Do work
}

int main(args)
{
    boost::thread blockThread(Block);

    //timed_join() returns false if the thread is still running after the specified time.
    if(!blockThread.timed_join(boost::posix_time::milliseconds(5000)))
    {   //detach() will kill the thread, any memory initialised in Block() will not be freed, any locals may or may not be freed either.
        blockThread.detach();
    }
}
Ephphatha