29#ifndef THREADS_INCLUDED
30#define THREADS_INCLUDED
36#include <condition_variable>
49#if defined( _WIN32 ) || defined( _WIN64 )
52template<
typename Value >
53bool SetAtomic32(
volatile Value *value , Value newValue , Value oldValue )
55#if defined( _WIN32 ) || defined( _WIN64 )
56 long &_oldValue = *(
long *)&oldValue;
57 long &_newValue = *(
long *)&newValue;
58 return InterlockedCompareExchange( (
long*)value , _newValue , _oldValue )==_oldValue;
60 uint32_t &_oldValue = *(uint32_t *)&oldValue;
61 uint32_t &_newValue = *(uint32_t *)&newValue;
63 return __atomic_compare_exchange_n( (uint32_t *)value , (uint32_t *)&oldValue , _newValue ,
false , __ATOMIC_SEQ_CST , __ATOMIC_SEQ_CST );
66template<
typename Value >
67bool SetAtomic64(
volatile Value *value , Value newValue , Value oldValue )
69#if defined( _WIN32 ) || defined( _WIN64 )
70 __int64 &_oldValue = *(__int64 *)&oldValue;
71 __int64 &_newValue = *(__int64 *)&newValue;
72 return InterlockedCompareExchange64( (__int64*)value , _newValue , _oldValue )==_oldValue;
74 uint64_t &_oldValue = *(uint64_t *)&oldValue;
75 uint64_t &_newValue = *(uint64_t *)&newValue;
77 return __atomic_compare_exchange_n( (uint64_t *)value , (uint64_t *)&oldValue , _newValue ,
false , __ATOMIC_SEQ_CST , __ATOMIC_SEQ_CST );
81template<
typename Value >
82bool SetAtomic(
volatile Value *value , Value newValue , Value oldValue )
84 switch(
sizeof(Value) )
86 case 4:
return SetAtomic32( value , newValue , oldValue );
87 case 8:
return SetAtomic64( value , newValue , oldValue );
89 WARN_ONCE(
"should not use this function: " ,
sizeof(Value) );
90 static std::mutex setAtomicMutex;
91 std::lock_guard< std::mutex > lock( setAtomicMutex );
92 if( *value==oldValue ){ *value = newValue ;
return true; }
120 template<
typename ... Functions >
123 std::vector< std::future< void > > futures(
sizeof...(Functions) );
125 for(
size_t t=0 ; t<futures.size() ; t++ ) futures[t].get();
132 static void Init(
ParallelType parallelType ,
unsigned int numThreads=std::thread::hardware_concurrency() );
140 template<
typename Function >
141 static void _ParallelSections( std::future< void > *futures ,
const Function &
function ){ *futures = std::async( std::launch::async ,
function ); }
143 template<
typename Function ,
typename ... Functions >
144 static void _ParallelSections( std::future< void > *futures ,
const Function &
function ,
const Functions& ... functions )
146 *futures = std::async( std::launch::async ,
function );
#define WARN_ONCE(...)
Definition: exceptions.h:148
static void _ParallelSections(std::future< void > *futures, const Function &function)
Definition: threads.h:141
ThreadPool(const ThreadPool &)
Definition: threads.h:137
static bool _Close
Definition: threads.h:151
static void Terminate(void)
Definition: threads.cpp:157
static void ParallelSections(const Functions &... functions)
Definition: threads.h:121
static std::function< void(unsigned int) > _ThreadFunction
Definition: threads.h:160
static ParallelType _ParallelType
Definition: threads.h:161
static void Parallel_for(size_t begin, size_t end, const std::function< void(unsigned int, size_t) > &iterationFunction, ScheduleType schedule=DefaultSchedule, size_t chunkSize=DefaultChunkSize)
Definition: threads.cpp:58
static std::condition_variable _WaitingForWorkOrClose
Definition: threads.h:158
ParallelType
Definition: threads.h:100
@ NONE
Definition: threads.h:101
@ THREAD_POOL
Definition: threads.h:105
@ ASYNC
Definition: threads.h:106
static const std::vector< std::string > ParallelNames
Definition: threads.h:108
static size_t DefaultChunkSize
Definition: threads.h:117
static std::mutex _Mutex
Definition: threads.h:157
static void _ThreadInitFunction(unsigned int thread)
Definition: threads.cpp:168
static void _ParallelSections(std::future< void > *futures, const Function &function, const Functions &... functions)
Definition: threads.h:144
static void Init(ParallelType parallelType, unsigned int numThreads=std::thread::hardware_concurrency())
Definition: threads.cpp:138
ThreadPool & operator=(const ThreadPool &)
Definition: threads.h:138
static std::vector< std::thread > _Threads
Definition: threads.h:159
static std::condition_variable _DoneWithWork
Definition: threads.h:158
static unsigned int _RemainingTasks
Definition: threads.h:153
ScheduleType
Definition: threads.h:111
@ STATIC
Definition: threads.h:112
@ DYNAMIC
Definition: threads.h:113
static ScheduleType DefaultSchedule
Definition: threads.h:118
static const std::vector< std::string > ScheduleNames
Definition: threads.h:115
static unsigned int NumThreads(void)
Definition: threads.cpp:136
bool SetAtomic32(volatile Value *value, Value newValue, Value oldValue)
Definition: threads.h:53
bool SetAtomic64(volatile Value *value, Value newValue, Value oldValue)
Definition: threads.h:67
bool SetAtomic(volatile Value *value, Value newValue, Value oldValue)
Definition: threads.h:82