I am looking for the best way to solve the following (c++) problem. I have a function given by some framework, which returns an object. Sometimes it takes just miliseconds, but on some occasions it takes minutes. So i want to stop the execution if it takes longer than let's say 2 seconds. I was thinking about doing it with boost threads. Important sidenote, if the function returns faster than the 2 seconds the program should not wait. So i was thinking about 2 threads:
1.thread: execute function a
2.thread: run timer
if(thread 2 exited bevore thread 1) kill thread 1
else do nothing
I am struggeling a bit the practical implementation. Especially,
- how do i return an object from a child boost thread to the main thread?
- how do i kill a thread in boost?
- is my idea even a good one, is there a better way to solve the problem in c++ (with or without boost)?