OpenTTD Source  1.11.2
thread.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef THREAD_H
11 #define THREAD_H
12 
13 #include "debug.h"
14 #include "crashlog.h"
15 #include <system_error>
16 #include <thread>
17 
22 inline void CSleep(int milliseconds)
23 {
24  std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
25 }
26 
31 void SetCurrentThreadName(const char *name);
32 
33 
44 template<class TFn, class... TArgs>
45 inline bool StartNewThread(std::thread *thr, const char *name, TFn&& _Fx, TArgs&&... _Ax)
46 {
47 #ifndef NO_THREADS
48  try {
49  std::thread t([] (const char *name, TFn&& F, TArgs&&... A) {
52  try {
53  /* Call user function with the given arguments. */
54  F(A...);
55  } catch (...) {
56  NOT_REACHED();
57  }
58  }, name, std::forward<TFn>(_Fx), std::forward<TArgs>(_Ax)...);
59 
60  if (thr != nullptr) {
61  *thr = std::move(t);
62  } else {
63  t.detach();
64  }
65 
66  return true;
67  } catch (const std::system_error& e) {
68  /* Something went wrong, the system we are running on might not support threads. */
69  DEBUG(misc, 1, "Can't create thread '%s': %s", name, e.what());
70  }
71 #endif
72 
73  return false;
74 }
75 
76 #endif /* THREAD_H */
SetCurrentThreadName
void SetCurrentThreadName(const char *name)
Name the thread this function is called on for the debugger.
Definition: os2.cpp:215
CSleep
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
Definition: thread.h:22
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
CrashLog::InitThread
static void InitThread()
Prepare crash log handler for a newly started thread.
Definition: crashlog_osx.cpp:261
StartNewThread
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:45
crashlog.h
debug.h