This is the mail archive of the pthreads-win32@sourceware.org mailing list for the pthreas-win32 project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Trying to develop a pool executor from pthreads... in C++


Hello,

Runner class should be inheriting the Runnable (to override the virtual
run() method)

-George Cherian


mlnglstaccram alksjd wrote:
> 
> For the sake of learning i am trying to develp a pool executor... but
> it doesnt work.. can u ppl tell me where i am goin wrong? I am getting
> an assertion failure in windows in the line below with comments, in
> file PoolExecutor.h..
> 
> 
> /*
> main.cpp
> */
> #include <pthread.h>
> #include <iostream>
> #include <windows.h>
> #include "PoolExecutor.h"
> 
> 
> 
> using namespace std;
> 
> class Runner{
> public:
>        Runner(){}
>        ~Runner(){}
>        void run(){
>                for(int i=0;i<100;++i)
>                        cout<<"i="<
> #include<queue>
> #include<windows.h>
> 
> class PoolExecutor;
> void *func(void *temp);
> 
> class Runnable{
> public:
>        virtual void run()=0;
> };
> 
> 
> class Thread{
>        pthread_t threadId;
>        Runnable *runnable;
> public:
>        Thread(){
>                runnable=NULL;
>        }
> 
>        void start(PoolExecutor *pthRun){
>                int started = pthread_create (&threadId, NULL, func,
> pthRun);
>                std::cout<< started<<std::endl;
>        }
> 
>        void join(){
>                pthread_join(threadId,NULL);
>        }
> 
> };
> 
> 
> 
> class PoolExecutor{
>        Thread *threads;
>        int N;
>        bool cancelled ;
>        std::queue<Runnable*> taskQueue;
>        pthread_mutex_t taskQueueMutex;
> public:
>        PoolExecutor(int numberOfThreads){
>                N=numberOfThreads;
>                if(numberOfThreads<=0 && numberOfThreads>512)
>                        N=5;
>                cancelled= false;
>        pthread_mutex_init(&taskQueueMutex,NULL);
>                threads = new Thread[N];
>                for(int i=0;i<N;++i)
>                        threads[i].start(this);
>        }
>        ~PoolExecutor(){
>                cancelled = true;
>                for(int i=0;i<N;++i)
>                        threads[i].join();
>                delete[] threads;
>        }
> 
>        void execute(Runnable* runnable ){
>                pthread_mutex_lock(&taskQueueMutex);
>                taskQueue.push(runnable);
>                pthread_mutex_unlock(&taskQueueMutex);
>        }
> 
>        void* runMethod(void * param){
>                for(;;){
>                        while(taskQueue.empty()&& !cancelled){
>                                Sleep(100);
>                        }
>                        if(cancelled){
>                                return NULL;
>                        }
>                        pthread_mutex_lock(&taskQueueMutex);
>                        if(taskQueue.empty()){
>                                pthread_mutex_unlock(&taskQueueMutex);
>                                continue;
>                        }
>                        Runnable *runnable = (Runnable *)taskQueue.front();
> ////////////////////////////////////////////////////////////////////////////////////////////////
> //////////////////////////////////// i get error in the above line
> ////////////////////////////////////////////////////////////////////////////////////////////////
>                        taskQueue.pop();
>                        pthread_mutex_unlock(&taskQueueMutex);
>                        runnable->run();
>                        delete runnable;
>                        if(cancelled){
>                                return NULL;
>                        }
>                }
>                return NULL;
>        }
> 
> };
> 
> void *func(void *temp){
>        PoolExecutor *poolExecutor = (PoolExecutor *)temp;
>        return poolExecutor->runMethod(NULL);
> }
> 
> Thanks
> 
> 

-- 
View this message in context: http://www.nabble.com/Trying-to-develop-a-pool-executor-from-pthreads...-in-C%2B%2B-tp13174112p21698291.html
Sent from the Sourceware - pthreads-win32 list mailing list archive at Nabble.com.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]