CARMA C++
RunnableTask.h
Go to the documentation of this file.
1 // $Id: RunnableTask.h,v 1.1 2010/12/13 21:06:32 eml Exp $
2 
3 #ifndef SZA_UTIL_RUNNABLETASK_H
4 #define SZA_UTIL_RUNNABLETASK_H
5 
16 #include "carma/szautil/Runnable.h"
17 
18 #include <iostream>
19 
20 namespace sza {
21  namespace util {
22 
23  class RunnableTask : public Runnable {
24  public:
25 
29  RunnableTask(bool spawnThread) :
30  Runnable(spawnThread, runFn)
31  {
32  // CAUTION: spawn() calls the startup function passed to
33  // pthread_create() when the thread corresponding to this
34  // object is created in Runnable(). This startup function
35  // calls our static runFn() method, which in turn calls the
36  // run() function of this object.
37  //
38  // If we call spawn() in the constructor, there's no
39  // guarantee that the virtual table for this object has been
40  // constructed by the time run() is called, which means that
41  // sometimes the call to run() will correctly call the
42  // overloaded version in the inherited class, but sometimes
43  // it will call the virtual base-class method below.
44  //
45  // Therefore, this construct is not safe as-is, and spawn()
46  // should NOT be called here. You should instead construct
47  // the object in external code, then call spawn() on it
48  // directly.
49 
50  // spawn(this);
51 
52  }
53 
57  virtual ~RunnableTask() {};
58 
59  virtual void run() {
60  };
61 
65  static RUN_FN(runFn) {
66  RunnableTask* runnable = (RunnableTask*) arg;
67  runnable->run();
68  return 0;
69  }
70 
71  }; // End class RunnableTask
72 
73  } // End namespace util
74 } // End namespace sza
75 
76 
77 
78 #endif // End #ifndef SZA_UTIL_RUNNABLETASK_H