64#define MINGW_STDTHREAD_REDUNDANCY_WARNING
74#include <condition_variable>
80#ifndef PARALLEL_THREAD_H_
81#define PARALLEL_THREAD_H_
92 template <
class C,
void (C::*Function)()>
93 void set(C* instance) {
98 template <u
int32_t s,
class C,
void (C::*Function)()>
99 void set(C* instance) {
118 template <
class C,
void (C::*Function)()>
120 return (
static_cast<C*
>(instance)->*Function)();
137 #if __cplusplus > 201703L
148 if(
pRun.load(std::memory_order_acquire) ) {
160 return (
pRun.load(std::memory_order_acquire) &&
190 if (maxDuration > 2) {
199 if (
getState())
pWait.store(
true, std::memory_order_release);
205 #if __cplusplus > 201703L
218 while (
pWait.load(std::memory_order_acquire)) {
224 if (maxDuration > 5) {
225 pWait.store(
false, std::memory_order_release);
239 pRun.store(
false, std::memory_order_release);
240 if (
pThd.joinable()) {
242 #if __cplusplus > 201703L
257 #if __cplusplus > 201703L
274 pthread_condattr_t cond_attr;
275 pthread_condattr_init(&cond_attr);
276 pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC);
277 pthread_cond_init(&
pProcCond, &cond_attr);
278 pthread_condattr_destroy(&cond_attr);
283 inline void run() noexcept {
284 if(
pRun.load(std::memory_order_acquire) ) {
287 pRun.store(
true, std::memory_order_release);
288 pThd = std::thread([
this]() {
289 #if __cplusplus <= 201703L
290 std::unique_lock<std::mutex> lk(
pWaitWork);
292 while (
pRun.load(std::memory_order_acquire)) {
293 isWaiting.store(
true, std::memory_order_release);
296 #if __cplusplus > 201703L
302 isWaiting.store(
false, std::memory_order_release);
303 pWait.store(
true, std::memory_order_release);
305 pWait.store(
false, std::memory_order_release);
313 return isWaiting.load(std::memory_order_acquire);
318 #if defined(__linux__) || defined(_UNIX) || defined(__APPLE__) || defined(_OS_UNIX_)
319 sched_param sch_params;
321 rt_prio = sched_get_priority_max(rt_policy);
323 if ((rt_prio/5) > 0) rt_prio = rt_prio/5;
324 sch_params.sched_priority = rt_prio;
325 if (pthread_setschedparam(
pThd.native_handle(), rt_policy, &sch_params)) {
326 fprintf(stderr,
"ParallelThread:%s fail to set priority %i shed %i\n",
threadName.c_str(), rt_prio, rt_policy);
328 #elif defined(_WIN32)
330 if (SetThreadPriority(
pThd.native_handle(), 24)) {
331 fprintf(stderr,
"ParallelThread:%s fail to set priority\n",
threadName.c_str());
340 clock_gettime (CLOCK_MONOTONIC, &
timeOut);
342 if (
timeOut.tv_nsec + at > 1000000000) {
352 int clock_gettime(
int,
struct timespec *spec) {
354 GetSystemTimePreciseAsFileTime((FILETIME*)&wTime);
355 wTime -=116444736000000000LL;
356 spec->tv_sec =wTime / 10000000LL;
357 spec->tv_nsec =wTime % 10000000LL *100;
std::atomic< bool > pWait
bool getProcess() noexcept
void setPriority(int32_t rt_prio, int32_t rt_policy) noexcept
void setThreadName(std::string name) noexcept
void processWait() noexcept
void setTimeOut(uint32_t timeout) noexcept
bool getState() const noexcept
std::atomic< bool > isWaiting
pthread_mutex_t pWaitProc
std::condition_variable pWorkCond
void setThreadPolicy(int32_t rt_prio, int32_t rt_policy) noexcept
void runProcess() noexcept
bool isRunning() const noexcept
struct timespec * getTimeOut() noexcept
static void wrap(InstancePtr instance)
void(* MemberFunc)(InstancePtr)
void setProcessor(uint32_t i_)