c++ threadpoolmain.cpp

//threadpoolmain.cpp


#include <iostream>   
#include "threadpool.h"   
   
using namespace std;   
   
   
#define ITERATIONS 200   
   
class SampleWorkerThread : public WorkerThread   
{   
public:   
        int id;   
   
        unsigned virtual executeThis()   
        {   
        // Instead of sleep() we could do anytime consuming work here.   
        //Using ThreadPools is advantageous only when the work to be done is really time consuming. (atleast 1 or 2 seconds)   
                sleep(2);   
   
                return(0);   
        }   
   
   
        SampleWorkerThread(int id) : WorkerThread(id), id(id)   
        {   
//           cout << "Creating SampleWorkerThread " << id << "\t address=" << this << endl;   
        }   
   
        ~SampleWorkerThread()   
        {   
//           cout << "Deleting SampleWorkerThread " << id << "\t address=" << this << endl;   
        }   
};   
   
   
int main(int argc, char **argv)   
{   
        //ThreadPool(N);   
        //Create a Threadpool with N number of threads   
        ThreadPool* myPool = new ThreadPool(25);   
        myPool->initializeThreads();   
   
        //We will count time elapsed after initializeThreads()   
    time_t t1=time(NULL);   
   
        //Lets start bullying ThreadPool with tonnes of work !!!   
        for(unsigned int i=0;i<ITERATIONS;i++){   
                SampleWorkerThread* myThread = new SampleWorkerThread(i);   
//cout << "myThread[" << myThread->id << "] = [" << myThread << "]" << endl;   
                myPool->assignWork(myThread);   
        }   
           
        // destroyPool(int maxPollSecs)   
        // Before actually destroying the ThreadPool, this function checks if all the pending work is completed.   
        // If the work is still not done, then it will check again after maxPollSecs   
        // The default value for maxPollSecs is 2 seconds.   
        // And ofcourse the user is supposed to adjust it for his needs.   
           
    myPool->destroyPool(2);   
   
    time_t t2=time(NULL);   
    cout << t2-t1 << " seconds elapsed\n" << endl;   
        delete myPool;   
           
    return 0;   

發佈了71 篇原創文章 · 獲贊 13 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章