From: gcherian <gcherian.142857@gmail.com>
To: pthreads-win32@sourceware.org
Subject: Re: Trying to develop a pool executor from pthreads... in C++
Date: Wed, 28 Jan 2009 01:28:00 -0000 [thread overview]
Message-ID: <21698291.post@talk.nabble.com> (raw)
In-Reply-To: <61e59ce30710120512i40fd0912t1df90a33c59e9f26@mail.gmail.com>
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.
prev parent reply other threads:[~2009-01-28 1:28 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-11-12 19:58 mlnglstaccram alksjd
2009-01-28 1:28 ` gcherian [this message]
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=21698291.post@talk.nabble.com \
--to=gcherian.142857@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).