RE: [Boost-users] boost::thread and std::system
Jerry Swan
I'd like to be able to issue a call to std::system that will time-out after a specified period. Is it possible to achieve this using the boost::threads library?
You can stop waiting for it to complete, but you can't terminate the external program.
If so could someone kindly sketch how this might be achieved?
You need to have the calling thread create a synchronised flag (bool with condition, protected by mutex) and a child thread that calls system() and then sets the flag when that returns. The original thread then performs a timed wait for the flag to be set. Here's my solution. The synchronised flag can be encapsulated as an "event" like this: struct one_time_event { public: one_time_event() : flag(false) { } void trigger() { boost::mutex::scoped_lock lock(mutex); flag = true; cond.notify_all(); } void wait() { boost::mutex::scoped_lock lock(mutex); while (!flag) cond.wait(lock); } bool timed_wait(boost::xtime & time) { boost::mutex::scoped_lock lock(mutex); while (!flag) if (!cond.timed_wait(lock, time)) return false; return true; } private: boost::mutex mutex; boost::condition cond; bool flag; }; (This is a basic event that you use once and then throw away. More complex event classes are possible but harder to use correctly.) Using this class it's really very easy: void call_system(const char * command, one_time_event & event) { std::system(command); event.trigger(); } bool system_with_time_limit(const char * command, boost::xtime & time) { one_time_event event; boost::thread thread( boost::bind(call_system, command, boost::ref(event))); return event.timed_wait(time); } Ben.
This works perfectly, thanks.
Jerry.
----- Original Message -----
From: "Ben Hutchings"
Jerry Swan
wrote: I'd like to be able to issue a call to std::system that will time-out after a specified period. Is it possible to achieve this using the boost::threads library?
You can stop waiting for it to complete, but you can't terminate the external program.
If so could someone kindly sketch how this might be achieved?
You need to have the calling thread create a synchronised flag (bool with condition, protected by mutex) and a child thread that calls system() and then sets the flag when that returns. The original thread then performs a timed wait for the flag to be set.
Here's my solution. The synchronised flag can be encapsulated as an "event" like this:
struct one_time_event { public: one_time_event() : flag(false) { } void trigger() { boost::mutex::scoped_lock lock(mutex); flag = true; cond.notify_all(); } void wait() { boost::mutex::scoped_lock lock(mutex); while (!flag) cond.wait(lock); } bool timed_wait(boost::xtime & time) { boost::mutex::scoped_lock lock(mutex); while (!flag) if (!cond.timed_wait(lock, time)) return false; return true; }
private: boost::mutex mutex; boost::condition cond; bool flag; };
(This is a basic event that you use once and then throw away. More complex event classes are possible but harder to use correctly.)
Using this class it's really very easy:
void call_system(const char * command, one_time_event & event) { std::system(command); event.trigger(); }
bool system_with_time_limit(const char * command, boost::xtime & time) { one_time_event event; boost::thread thread( boost::bind(call_system, command, boost::ref(event))); return event.timed_wait(time); }
Ben.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Ben Hutchings
-
Jerry Swan