8 #include <sys/sysctl.h> 12 #ifndef PFC_WINDOWS_DESKTOP_APP 18 #ifdef PFC_WINDOWS_DESKTOP_APP 19 DWORD_PTR mask,system;
21 GetProcessAffinityMask(GetCurrentProcess(),&mask,&system);
22 for(
t_size n=0;n<
sizeof(mask)*8;n++) {
23 if (mask & ((DWORD_PTR)1<<n)) ret++;
25 if (ret == 0)
return 1;
28 return std::thread::hardware_concurrency();
37 sysctlbyname (
"hw.ncpu",&ncpu,&len,NULL,0);
44 if (taskCountLimit <= 1)
return 1;
63 int ctxPriority = GetThreadPriority( GetCurrentThread() );
64 if (ctxPriority > GetThreadPriority(
m_thread ) ) SetThreadPriority(
m_thread, ctxPriority );
71 return m_thread != INVALID_HANDLE_VALUE;
76 #ifdef PFC_WINDOWS_DESKTOP_APP 77 thread = (
HANDLE)_beginthreadex(NULL, 0,
g_entry, reinterpret_cast<void*>(
this), CREATE_SUSPENDED, (
unsigned int*)outThreadID);
79 thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)
g_entry, reinterpret_cast<void*>(
this), CREATE_SUSPENDED, outThreadID);
81 if (thread == NULL)
throw exception_creation();
82 SetThreadPriority(thread, priority);
91 SetThreadPriority(
m_thread, priority);
98 reinterpret_cast<thread*
>(p_instance)->
entry();
return 0;
103 return GetThreadPriority(
m_thread );
107 return GetThreadPriority( GetCurrentThread() );
113 #ifndef __APPLE__ // Apple specific entrypoint in obj-c.mm 127 pthread_attr_init(&attr);
129 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
131 if (pthread_create(&thread, &attr,
g_entry, reinterpret_cast<void*>(
this)) < 0)
throw exception_creation();
133 pthread_attr_destroy(&attr);
T min_t(const T &item1, const T &item2)
t_size getOptimalWorkerThreadCountEx(t_size taskCountLimit)
void startWithPriority(int priority)
t_size getOptimalWorkerThreadCount()
typedef HANDLE(WINAPI *pPowerCreateRequest_t)(__in void *Context)
IMPORTANT: all classes derived from thread must call waitTillDone() in their destructor, to avoid object destruction during a virtual function call!
DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
void setPriority(int priority)
virtual void threadProc()
static unsigned CALLBACK g_entry(void *p_instance)
static int currentPriority()
static void appleStartThreadPrologue()
void winStart(int priority, DWORD *outThreadID)