Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
threadspawner.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_THREADSPAWNER_H_
19 #define ION_BASE_THREADSPAWNER_H_
20 
21 #include <string>
22 
23 #include "ion/port/threadutils.h"
24 
25 namespace ion {
26 namespace base {
27 
31  public:
34  ThreadSpawner(const std::string& name, port::ThreadFuncPtr func_ptr);
35  ThreadSpawner(const std::string& name, const port::ThreadStdFunc& func);
36 
39 
42  void Join();
43 
45  const std::string& GetName() const { return name_; }
46 
50  port::ThreadId GetId() const { return id_; }
51 
52  private:
54  port::ThreadId Spawn();
55 
57  const std::string name_;
60  const port::ThreadStdFunc func_;
63  port::ThreadId id_;
64 };
65 
66 } // namespace base
67 } // namespace ion
68 
69 #endif // ION_BASE_THREADSPAWNER_H_
bool(* ThreadFuncPtr)()
Thread types and constants.
Definition: threadutils.h:58
const std::string & GetName() const
Returns the name of the thread supplied to the constructor.
Definition: threadspawner.h:45
A ThreadSpawner instance launches a new thread in its constructor and waits for the thread to finish ...
Definition: threadspawner.h:30
void Join()
Waits for the thread to finish.
~ThreadSpawner()
The destructor calls Join() to wait for the thread to finish.
port::ThreadId GetId() const
Returns the ThreadId for the thread.
Definition: threadspawner.h:50
std::string name
Definition: printer.cc:324
std::function< bool()> ThreadStdFunc
Definition: threadutils.h:59
pthread_t ThreadId
Defines a type that can be used to identify a thread.
Definition: threadutils.h:40
ThreadSpawner(const std::string &name, port::ThreadFuncPtr func_ptr)
Creates a ThreadSpawner instance that runs the given function.