Is the following use of boost::function<> safe?

Let me preface this with the note that the whole idea behind what I'm
trying to do is invoke an arbitrary function in a new thread, optionally
delaying the invocation by some number of milliseconds
That said,
----- threadrun.h -----
#ifndef _THREADRUN_H_
#define _THREADRUN_H_
#include

AMDG On 12/09/2013 12:09 AM, David Goodenough wrote:
namespace Thread { void ThreadRun(boost::function
const &job); void ThreadDelayedRun(boost::function const &job, unsigned int delay); }; <snip>
int myFunc1(int a, int b) { ... return x } <snip> My only concern is the second case where I have myFunc1() whose return type is int, yet in the prototype for ThreadDelayedRun() I'm making it look a whole lot like it's void.
That isn't a problem at all. boost::function
The return type ultimately doesn't matter since this is specifically designed to be a "fire and forget" system, which means that any return value will be lost, there's no way for the invoking thread to recover it.
In Christ, Steven Watanabe

void ThreadRun(boost::function
const &job) { boost::thread thr = boost::thread(job); thr.detach(); // Not really necessary, but it does make it explicit that we'll never wait for the thread to finish }
Note that detaching or joining *is* necessary since v2 - otherwise thread object destructor would call std::terminate(): http://www.boost.org/doc/libs/1_55_0/doc/html/thread/thread_management.html#...
participants (3)
-
David Goodenough
-
Igor R
-
Steven Watanabe