All pastes #950347 Raw Edit

Stuff

public cpp v1 · immutable
·published 2008-03-20 14:43 UTC
rendered paste body
class ThreadPool{private:	pthread_mutex_t mutex;	pthread_cond_t cond;	bool f;	PoolThread[] threads;	Object[] tasks;	class PoolThread : public Thread	{		bool running;		int id;		int run()		{			pthread_mutex_lock(&mutex);			running = true;			Object task;			while(running) {				if (tasks.length > 0) {					task = tasks[length - 1];					tasks.length = tasks.length - 1;					pthread_mutex_unlock(&mutex);					writefln(id, " ", task); // Do the task					pthread_mutex_lock(&mutex);				} else {					pthread_cond_wait(&cond, &mutex);				}			}			pthread_mutex_unlock(&mutex);			return 0;		}	}public:	this(size_t num)	{		pthread_mutex_init(&mutex, null);		pthread_cond_init(&cond, null);		threads.length = num;		for (size_t i; i < num; i++) {			threads[i] = new PoolThread();			threads[i].id = i;			threads[i].start();			while(!threads[i].running) {}		}	}	void task(Object task)	{		pthread_mutex_lock(&mutex);		tasks ~= task;		pthread_cond_broadcast(&cond);		pthread_mutex_unlock(&mutex);	}}