#ifndef THREAD_POOL#define THREAD_POOL#include<boost/thread.hpp>#include<boost/thread/mutex.hpp>#include<boost/signals2/signal.hpp>#include<queue>#include<iostream>#include"transport/logging.h"#include"Swiften/Swiften.h"/* * Thread serves as a base class for any code that has to be excuted as a thread * by the ThreadPool class. The run method defines the code that has to be run * as a theard. For example, code in run could be sendinga request to a server * waiting for the response and storing the response. When the thread finishes * execution, the ThreadPool invokes finalize where one could have the code necessary * to collect all the responses and release any resources. * * NOTE: The object of the Thread class must be valid (in scope) throughout the * execution of the thread. */classThread{intthreadID;public:Thread(){}virtual~Thread(){}virtualvoidrun()=0;virtualvoidfinalize(){}intgetThreadID(){returnthreadID;}voidsetThreadID(inttid){threadID=tid;}};/* * ThreadPool provides the interface to manage a pool of threads. It schedules jobs * on free threads and when the thread completes it automatically deletes the object * corresponding to a Thread. If free threads are not available, the requests are * added to a queue and scheduled later when threads become available. */classThreadPool{constintMAX_THREADS;intactiveThreads;std::queue<int>freeThreads;std::queue<Thread*>requestQueue;boost::thread**worker;boost::mutexcount_lock;boost::mutexpool_lock;boost::mutexcriticalregion;Swift::EventLoop*loop;boost::signals2::signal<void()>onWorkerAvailable;public:ThreadPool(Swift::EventLoop*loop,intmaxthreads);~ThreadPool();voidrunAsThread(Thread*t);intgetActiveThreadCount();voidupdateActiveThreadCount(intk);voidcleandUp(Thread*,int);voidscheduleFromQueue();intgetFreeThread();voidreleaseThread(inti);};#endif