Link to home
Start Free TrialLog in
Avatar of bachra04
bachra04Flag for Canada

asked on

add timeout to a function call

I need to call a library function that sometimes won't terminate within a given time, unfortunately. Is there a way to call the function but abort it if it doesn't terminate within n seconds?
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi bachra,

You can't terminate a function, but you can terminate a thread.  Spawn a thread and call the function from the new thread.  The main thread can act as your "Traffic Cop" and if the function takes too long to complete it kills the new thread.


Kent
What OS are you targetting?
Avatar of bachra04

ASKER

linux
it is rarely a good idea to kill a thread while executing. in many cases you may cause severe problems by this, for example that resources are not freed, that mutexes or events do not work anymore, that files keep open or cannot deleted, more ...

if you have a function that should end on request you may spawn it in a thread and pass a pointer to a boolean (or a pointer to a structure with a boolean as member). if you declare the boolean as volatile, the boolean is a shared variable then, and the main thread and the worker thread now can communicate using that variable.

if the main thread wants the thread to terminate, it sets the boolean to true (which initially is false). the thread would check the boolean periodically (for example at each iteration in a loop) and would terminate when it recognized the boolean to become true.

the main thread in the meantime would wait for the thread to smoothly end, for example by waiting on the thread handle to become signaled.

Sara
Avatar of HooKooDooKu
HooKooDooKu

Sara has some good advice... if you have access to the source code for the library function.

If you don't, then you're limited to something more along the lines of the "Traffic Cop" wrapper function.  But as Sara points out, you don't want to be killing threads unless you absolutely must.

So a possible middle ground could be a "Traffic Cop" class.  In the class, each time you need to call the library function, the Traffic Cop class could allocate memory for a return value for the library function, and spawn a thread to execute the library function.  If the library function completes within the desired time, you clean up the thread and the allocated memory and return the result from the traffic cop.  If the library function fails to complete within the desired time, you then have the Traffic Cop spawn another "cleanup" thread.  The purpose of the cleanup thread would be to simply sit and wait for the library thread to complete, then clean everything up for that thread and itself.  In the mean time, you can return control to the calling function with a failure result.

This Traffic Cop thread would also track all the threads that it has spawned.  If the Traffic Cop object needs to be destroyed (such as the application is closing - especially if it is closing because a call from the OS indicating the OS is shutting down) the destructor could then send a message to each of the cleanup threads (like the boolean Sara talked about) that would tell the cleanup threads to kill any unfinished library threads and then exit.  Once all the cleanup threads have finished, return from the destructor.

That way, you get the best worlds of the Traffic Cop id, while normally keeping your system "clean" like Sara suggests.
Hi Sara, HooDooKu

I like the idea of Traffic Cop

So a possible middle ground could be a "Traffic Cop" class.  In the class, each time you need to call the library function, the Traffic Cop class could allocate memory for a return value for the library function, and spawn a thread to execute the library function.  If the library function completes within the desired time, you clean up the thread and the allocated memory and return the result from the traffic cop.  If the library function fails to complete within the desired time, you then have the Traffic Cop spawn another "cleanup" thread.  The purpose of the cleanup thread would be to simply sit and wait for the library thread to complete, then clean everything up for that thread and itself.  In the mean time, you can return control to the calling function with a failure result.


But in order to translate that to code, it does not look trivial to me. Do you have any sample code of traffic cop that I can start with ?
Sorry, I don't have any sample code.  You basically have to learn the basics of multi-threading.  That's one of those subjects that when you're not used to it, it can seem quite daunting.  But there's only a few things that are needed to know.

I picked up everything I needed to know by reading a chapter on "Threads and Thread Synchronization" in the book "Programming Windows with MFC" by Jeff Prosise.  The book is old enough now that you should be able to find similar information online about learning to do basic multi-threading.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial