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;   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值