#ifdef _MULTITHREADED class CoWork : NoCopy { typedef StaticCriticalSection Lock; struct MJob : Moveable { Function fn; CoWork *work; }; enum { SCHEDULED_MAX = 2048 }; public: struct Pool { int scheduled; MJob jobs[SCHEDULED_MAX]; int waiting_threads; Array threads; Mutex lock; Semaphore waitforjob; Pool(int nthreads); ~Pool(); static thread__ bool finlock; static bool DoJob(); static void ThreadRun(int tno); }; friend struct Pool; static Pool& GetPool(int n); static Pool& GetPool(); static thread_local bool is_worker; static thread_local Pool *pool; Semaphore waitforfinish; int todo; Mutex stepmutex; Array>> step; Vector steprunning; public: void Do(Function&& fn); void Do(const Function& fn) { Do(clone(fn)); } CoWork& operator&(const Function& fn) { Do(fn); return *this; } CoWork& operator&(Function&& fn) { Do(pick(fn)); return *this; } void Pipe(int stepi, Function&& lambda); static void FinLock(); void Finish(); bool IsFinished(); static bool IsWorker() { return is_worker; } static void StartPool(int n); static void ShutdownPool(); CoWork(); ~CoWork(); }; #else class CoWork : NoCopy { public: void Do(Callback cb) { cb(); } CoWork& operator&(Callback cb) { cb(); return *this; } void Finish() {} bool IsFinished() { return true; } static void FinLock() {} static bool IsWorker() { return false; } static void StartPool(int n) {} static void ShutdownPool() {} }; #endif