public inbox for pthreads-win32@sourceware.org
 help / color / mirror / Atom feed
From: "mlnglstaccram alksjd" <mlnglstaccram@gmail.com>
To: pthreads-win32@sourceware.org
Subject: Trying to develop a pool executor from pthreads... in C++
Date: Mon, 12 Nov 2007 19:58:00 -0000	[thread overview]
Message-ID: <61e59ce30710120512i40fd0912t1df90a33c59e9f26@mail.gmail.com> (raw)

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="<<i << " in thread "<<
((int)pthread_self().p) <<endl;
       }
};

int main(){
       PoolExecutor pool(10);
       pool.execute(((Runnable *)new Runner()));
       pool.execute(((Runnable *)new Runner()));
       pool.execute(((Runnable *)new Runner()));
       Sleep(1000);
       return 0;
}

///////////////////////////////////////////////////////////////////////////next
file//////////////////////////////////////////

/*in file PoolExecutor.h*/

#include<pthread.h>
#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

             reply	other threads:[~2007-10-12 12:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-12 19:58 mlnglstaccram alksjd [this message]
2009-01-28  1:28 ` gcherian

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=61e59ce30710120512i40fd0912t1df90a33c59e9f26@mail.gmail.com \
    --to=mlnglstaccram@gmail.com \
    --cc=pthreads-win32@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).