C ++ Windows Thread Pool (non-boost / C ++ 11)

Is there a way to create threadpool only using C ++ or Windows C ++ functions? I don’t have access to boost or any libraries (I can access the code project, but couldn’t find anything non-unix) and it’s hard for me to find a way to implement threadpool.

I am using VS2010, which does not yet support C ++ 11 streams, so why am I a bit stuck!

+2
source share
4 answers

If your goal is Windows Vista or later, you can use this thread pool: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686980%28v=vs.85%29.aspx

The page also has an example in C ++.

+3
source

This is pretty easy. You need several classes:

A task class with a "run" method is something for a pool user to override his own tasks.

Consumer-consumer-producer object for threads awaiting work. If you have std :: deque, this is pretty easy. If not, you will have to code your own queue type. Besides the queue class, you will need synchronization data, perhaps CriticalSection / Mutex to protect the queue and semaphore for the threads that are waiting.

The threadPool class is what contains the PC queue, submit (TaskClass aTask) and other things.

A bunch of threads created in threadPool ctor - use CreateThread and pass the threadPool instance as the lpParam parameter so that threads can return it back to access threadPool.

Threads are waiting on threadPool-> objectQueue to work, for example:

// header

class threadPool; class task { friend class threadPool; private: threadPool *myPool; public: virtual void run()=0; }; class PCSqueue{ private: CRITICAL_SECTION access; deque<task*> *objectQueue; HANDLE queueSema; public: PCSqueue(); void push(task *ref); bool pop(task **ref,DWORD timeout); }; class threadPool { private: int threadCount; public: PCSqueue *queue; threadPool(int initThreads); static DWORD _stdcall staticThreadRun(void *param){ threadPool *myPool=(threadPool *)param; task *thisTask; while (myPool->queue->pop(&thisTask,INFINITE)){ thisTask->run(); } } void submit(task *aTask); }; 

// cpp

 PCSqueue::PCSqueue(){ objectQueue=new deque<task*>; InitializeCriticalSection(&access); queueSema=CreateSemaphore(NULL,0,MAXINT,NULL); }; void PCSqueue::push(task *ref){ EnterCriticalSection(&access); objectQueue->push_front(ref); LeaveCriticalSection(&access); ReleaseSemaphore(queueSema,1,NULL); }; bool PCSqueue::pop(task **ref,DWORD timeout){ if (WAIT_OBJECT_0==WaitForSingleObject(queueSema,timeout)) { EnterCriticalSection(&access); *ref=objectQueue->back(); objectQueue->pop_back(); LeaveCriticalSection(&access); return(true); } else return(false); }; threadPool::threadPool(int initThreads){ queue=new PCSqueue(); for(threadCount=0;threadCount!=initThreads;threadCount++){ CreateThread(NULL,0,staticThreadRun,this,0,0); }; }; void threadPool::submit(task *aTask){ aTask->myPool=this; queue->push(aTask); }; 
+3
source

There is an API for managing thread pools, here is the link: http://msdn.microsoft.com/en-us/library/windows/ desktop / ms686766 (v = vs .85) .aspx . You might want to find a library that wraps it, because it's complicated.

+1
source

Source: https://habr.com/ru/post/1434775/


All Articles