A simple example of a Windows API application pool

[EDIT: thanks to MSalters and Raymond Chen's answer, answer to InterlockedIncrement vs EnterCriticalSection / counter ++ / LeaveCriticalSection , the problem is resolved and the code below works correctly. This should provide an interesting simple example of using a thread pool on Windows]

I am unable to find a simple example of the following task. For example, my program needs to increase the values ​​in a huge std :: vector by one, so I want to do this in parallel. He must do this several times throughout the life of the program. I know how to do this with CreateThread every time I call a procedure, but I cannot get rid of CreateThread with ThreadPool.

That's what I'm doing:

class Thread {
public:
    Thread(){}
    virtual void run() = 0 ; // I can inherit an "IncrementVectorThread"
};
class IncrementVectorThread: public Thread {
public:
   IncrementVectorThread(int threadID, int nbThreads, std::vector<int> &vec) : id(threadID), nb(nbThreads), myvec(vec) { };

   virtual void run() {
        for (int i=(myvec.size()*id)/nb; i<(myvec.size()*(id+1))/nb; i++)
          myvec[i]++; //and let assume myvec is properly sized
    }
   int id, nb;
   std::vector<int> &myvec;
};

class ThreadGroup : public std::vector<Thread*> {
public:
    ThreadGroup() { 
         pool = CreateThreadpool(NULL);
         InitializeThreadpoolEnvironment(&cbe);
         cleanupGroup = CreateThreadpoolCleanupGroup();
         SetThreadpoolCallbackPool(&cbe, pool);
         SetThreadpoolCallbackCleanupGroup(&cbe, cleanupGroup, NULL);
         threadCount = 0;
    }
    ~ThreadGroup() {
         CloseThreadpool(pool);
}
    PTP_POOL pool;
    TP_CALLBACK_ENVIRON cbe;
    PTP_CLEANUP_GROUP cleanupGroup;
    volatile long threadCount;
} ;


static VOID CALLBACK runFunc(
                PTP_CALLBACK_INSTANCE Instance,
                PVOID Context,
                PTP_WORK Work) {

   ThreadGroup &thread = *((ThreadGroup*) Context);
   long id = InterlockedIncrement(&(thread.threadCount));
   DWORD tid = (id-1)%thread.size();
   thread[tid]->run();
}

void run_threads(ThreadGroup* thread_group) {
    SetThreadpoolThreadMaximum(thread_group->pool, thread_group->size());
    SetThreadpoolThreadMinimum(thread_group->pool, thread_group->size());

    TP_WORK *worker = CreateThreadpoolWork(runFunc, (void*) thread_group, &thread_group->cbe);
    thread_group->threadCount = 0;
    for (int i=0; i<thread_group->size(); i++) {
        SubmitThreadpoolWork(worker);
     }  
     WaitForThreadpoolWorkCallbacks(worker,FALSE);  
     CloseThreadpoolWork(worker);   
}       

void main() {

   ThreadGroup group;
   std::vector<int> vec(10000, 0);
   for (int i=0; i<10; i++)
      group.push_back(new IncrementVectorThread(i, 10, vec));

   run_threads(&group);
   run_threads(&group);
   run_threads(&group);

   // now, vec should be == std::vector<int>(10000, 3);       
}

, :
- CreateThreadpool (, CreateThreadpoolWork , CreateThread)
- , ( "IncrementVector" "DecrementVector", ).
- " " 10 , , 10 CreateThread, "" 10 ThreadPool (, ID , , std::vector ). , GetCurrentThreadId() (.. - 1528, - 0..nb_launched_threads).

, , : , 10, std::vector 10 ?

!

+4
1

.

, , . , . , 10 , 1 10 .

. , , .

, , ? . threadpool, . InterlockedIncrement . .

+5

All Articles