My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
RunnableTask.h
Go to the documentation of this file.
1 // $Id: RunnableTask.h,v 1.1.1.1 2009/07/06 23:57:26 eml Exp $
2 
3 #ifndef GCP_UTIL_RUNNABLETASK_H
4 #define GCP_UTIL_RUNNABLETASK_H
5 
16 #include "gcp/util/common/Runnable.h"
17 
18 #include <iostream>
19 
20 namespace gcp {
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  }
69 
70  }; // End class RunnableTask
71 
72  } // End namespace util
73 } // End namespace gcp
74 
75 
76 
77 #endif // End #ifndef GCP_UTIL_RUNNABLETASK_H
Definition: RunnableTask.h:23
RunnableTask(bool spawnThread)
Definition: RunnableTask.h:29
static RUN_FN(runFn)
Definition: RunnableTask.h:65
Definition: Runnable.h:23
virtual ~RunnableTask()
Definition: RunnableTask.h:57