/* ------------------------------ USAGE ------------------------------ >>
1. Insert ThreadPool.cpp & .h in project.
2. Project Settings
- On the Project menu, Click Settings.
- In the Project Settings dialog box, click the C/C++ tab.
- Select Code Generation from the Category drop-down list box.
- From the Use Run-Time Library drop-down box, select MULTITHREADED.
- Click OK.
3. Constuct - Max threads, tasks can be recommended. Tasks list size
will grow if required.
4. Call Initialize() with required thread count.
5. Assign Task with a _cdecl function pointer of the client.
6. Function will be executed from any of the free thread in thread pool.
7. If DATA members need to be sync, in the submitted function in client,
that has to be done by the client.
8. When thread count need to be increased/decreased, just call Initialize again.
NO Resource/Memory LEAK. The current threads finishes executuion.
Then it will be aborted.
9. To destroy the thread pool, without deleting threadpool object,
call UnInitialize(). This will delete all resources. If threads are still
working, after WM_QUIT posting & a small waiting, it terminates all threads.
10. UnInitialize() is called from destructor() also.
<< ------------------------------ USAGE ---------------------------
*/
// -------Common External Data structures---------------
typedef unsigned int (__stdcall* THREAD_PROC)(void *);
typedef struct _tagTHREAD_DATA
{
unsigned long hThread; // Thread handle.
unsigned int nThreadId; // Thread Id.
unsigned int nFlags; // Thread status such as marked for exit.
}
THREAD_DATA, *PTHREAD_DATA;
class CThreadPool
{
queue<PTHREAD_POOL_QUEUE_INFO> m_TaskQueue; // Task queue.
int *m_aFreeIndex;
THREAD_DATA * m_aThreadData;
UINT m_cntThreads;
PTHREAD_POOL_QUEUE_INFO m_aTasks; // Task Data block.
UINT m_ixNextTask;
HANDLE m_hTaskQueueSync;
HANDLE m_hClientSync;
A Thread Pool compatible with Win32 and pthreads API
By Thomas George
A cross-platform thread pooling implementation
http://www.codeproject.com/threads/threads.asp