#ifdef _MULTITHREADED

class CoWork : NoCopy {

	struct MJob : Moveable<MJob> {
		Callback cb;
		CoWork  *work;
	};

	struct Pool {
		typedef Pool CLASSNAME;
		Vector<MJob>    jobs;
		int             waiting_threads;
		Array<Thread>   threads;

		CriticalSection lock;
		Semaphore       waitforjob;

		Pool(int threadnr = -1);
		~Pool();

		bool DoJob();
		void ThreadRun(int tno);
	};
	
	friend struct Pool;

	One<Pool> _opool;    //local pool, needs to be here, before _pool, to be initialized before
	static Pool& pool(); //global pool, switch in CoWork()
	Pool & _pool;        //this ref is actually used, set depending on threadnr

	Semaphore waitforfinish;
	int       todo;

public:
	void     Do(Callback cb);
	CoWork&  operator&(Callback cb) { Do(cb); return *this; }

	void Finish();

	CoWork(int threadnr = -1);
	~CoWork();
};

#else

class CoWork : NoCopy {
public:
	void     Do(Callback cb)        { cb(); }
	CoWork&  operator&(Callback cb) { cb(); return *this; }
	void     Finish()               {}
	CoWork(int threadnr = -1)       {}
};

#endif

class WorkQueue : public CoWork
{
public:
	WorkQueue()
		: CoWork(1)
	{}	
};
