37 #ifndef G4Threading_hh
38 #define G4Threading_hh
46 #include <condition_variable>
52 #define G4THREADSLEEP(tick) \
53 std::this_thread::sleep_for(std::chrono::seconds( tick ))
56 template <
typename _Tp>
using G4Future = std::future<_Tp>;
58 template <
typename _Tp>
using G4Promise = std::promise<_Tp>;
88 #define G4MUTEX_INITIALIZER {}
89 #define G4MUTEXINIT(mutex) ;;
90 #define G4MUTEXDESTROY(mutex) ;;
93 namespace G4ThisThread {
using namespace std::this_thread; }
97 template <
typename _Tp>
using G4Promise = std::promise<_Tp>;
98 template <
typename _Tp>
using G4Future = std::future<_Tp>;
99 template <
typename _Tp>
using G4SharedFuture = std::shared_future<_Tp>;
113 template <
typename _Tp>
120 static std::vector<G4Mutex*> _mutexes;
121 if(_n > _mutexes.size())
122 _mutexes.resize(_n,
nullptr);
125 return *(_mutexes[_n-1]);
134 template <
typename _Tp>
141 static std::vector<G4RecursiveMutex*> _mutexes;
142 if(_n > _mutexes.size())
143 _mutexes.resize(_n,
nullptr);
146 return *(_mutexes[_n-1]);
149 #if defined(G4MULTITHREADED)
159 #define G4MUTEXLOCK(mutex) { (mutex)->lock(); }
160 #define G4MUTEXUNLOCK(mutex) { (mutex)->unlock(); }
163 #define G4THREADJOIN(worker) (worker).join()
166 using G4Pid_t = std::thread::id;
170 template <
typename _Worker,
typename _Func,
typename... _Args>
173 *worker =
G4Thread(func, std::forward<_Args>(args)...);
181 #define G4CONDITION_INITIALIZER {}
182 #define G4CONDITIONWAIT(cond, lock) (cond)->wait(*lock);
183 #define G4CONDITIONWAITLAMBDA(cond, lock, lambda) (cond)->wait(*lock, lambda);
184 #define G4CONDITIONNOTIFY(cond) (cond)->notify_one();
185 #define G4CONDITIONBROADCAST(cond) (cond)->notify_all();
201 using id = std::thread::id;
208 template <
typename _Func,
typename... _Args>
211 func(std::forward<_Args>(_args)...);
217 id get_id() const noexcept {
return std::this_thread::get_id(); }
223 static unsigned int hardware_concurrency() noexcept
225 return std::thread::hardware_concurrency();
234 #define G4MUTEXLOCK(mutex) ;;
235 #define G4MUTEXUNLOCK(mutex) ;;
238 #define G4THREADJOIN(worker) ;;
244 template <
typename _Worker,
typename _Func,
typename... _Args>
247 *worker =
G4Thread(func, std::forward<_Args>(args)...);
251 #define G4CONDITION_INITIALIZER 1
252 #define G4CONDITIONWAIT(cond, mutex) G4ConsumeParameters(cond, mutex);
253 #define G4CONDITIONWAITLAMBDA(cond, mutex, lambda) G4ConsumeParameters(cond, mutex, lambda);
254 #define G4CONDITIONNOTIFY(cond) G4ConsumeParameters(cond);
255 #define G4CONDITIONBROADCAST(cond) G4ConsumeParameters(cond);
257 #endif //G4MULTITHREADING
266 namespace G4Threading
290 #endif //G4Threading_hh