Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
threadspawner.cc
Go to the documentation of this file.
1 
18 #include "ion/base/threadspawner.h"
19 
20 namespace ion {
21 namespace base {
22 
23 namespace {
24 
28 static bool NamingThreadFunc(const std::string& name,
29  const port::ThreadStdFunc& user_func) {
31  port::SetThreadName(name);
32  return user_func();
33 }
34 
35 } // anonymous namespace
36 
37 ThreadSpawner::ThreadSpawner(const std::string& name,
38  port::ThreadFuncPtr func_ptr)
39  : name_(name),
40  func_(func_ptr ? std::bind(NamingThreadFunc, name,
41  port::ThreadStdFunc(func_ptr))
42  : port::ThreadStdFunc()),
43  id_(Spawn()) {
44 }
45 
46 ThreadSpawner::ThreadSpawner(const std::string& name,
47  const port::ThreadStdFunc& func)
48  : name_(name),
49  func_(std::bind(NamingThreadFunc, name, func)),
50  id_(Spawn()) {
51 }
52 
54  Join();
55 }
56 
58  if (id_ != port::kInvalidThreadId) {
59  port::ThreadId id = id_;
60  id_ = port::kInvalidThreadId;
61  port::JoinThread(id);
62  }
63 }
64 
65 port::ThreadId ThreadSpawner::Spawn() {
66  if (func_)
67  return port::SpawnThreadStd(&func_);
68  else
69  return port::kInvalidThreadId;
70 }
71 
72 
73 } // namespace base
74 } // namespace ion
bool(* ThreadFuncPtr)()
Thread types and constants.
Definition: threadutils.h:58
bool JoinThread(ThreadId id)
Windows-specific public functions.
Definition: threadutils.cc:322
void Join()
Waits for the thread to finish.
ThreadId SpawnThreadStd(const ThreadStdFunc *func)
Definition: threadutils.cc:200
~ThreadSpawner()
The destructor calls Join() to wait for the thread to finish.
std::string name
Definition: printer.cc:324
std::function< bool()> ThreadStdFunc
Definition: threadutils.h:59
bool IsThreadNamingSupported()
Thread naming functions.
Definition: threadutils.cc:213
pthread_t ThreadId
Defines a type that can be used to identify a thread.
Definition: threadutils.h:40
bool SetThreadName(const std::string &name)
Sets the name of the current thread.
Definition: threadutils.cc:340
ThreadSpawner(const std::string &name, port::ThreadFuncPtr func_ptr)
Creates a ThreadSpawner instance that runs the given function.