# Creating Thread pool using ACE Programming



## sathavale (Oct 28, 2009)

I created a simple thread pool using ACE classes. The code is provided below. It breaks. 

//===========================================================
// Sample Thread Pool
//
//===========================================================

#include "ace/OS_main.h"
#include "ace/Task.h"
#include <iostream>

using namespace std;

class HA_CommandHandler:
public ACE_Task<ACE_MT_SYNCH>
{
public:
virtual int svc()
{
cout << "Starting" << endl;
ACE_DEBUG((LM_DEBUG, "(%t) starting up \n"));
ACE_Message_Block *mb;
int result = this->getq(mb);
//... do something with the message.
return 0;
}

};

int main(int argc, char *argv[])
{
cout << "In Main" << endl;
printf("In Main");
cout.flush();

HA_CommandHandler handler;
handler.activate( THR_NEW_LWP | THR_JOINABLE, 4);
create 4 threads
handler.wait();
return 0;
}

--------------------------------------------------------------------------

The moment I create an object of HA_CommandHandler in main, I can't print any output. 

cout << "In Main" << endl; doesn't print. 


There are no compiler issues

$ make
make[1]: Entering directory `/cygdrive/c/ACE-5.7.0/ACE_wrappers/tests/SThreadPool'

GNUmakefile: /cygdrive/c/ACE-5.7.0/ACE_wrappers/tests/SThreadPool/GNUmakefile.sThreadPool MA
KEFLAGS=w

g++ -Wpointer-arith -mthreads -mtune=pentiumpro -O3 -g -pipe -pipe -I/cygdrive/c/ACE-5.
7.0/ACE_wrappers -DACE_HAS_EXCEPTIONS -DACE_NO_INLINE -I.. -c -o .obj/sThreadPool.o sThread
Pool.cpp
g++ -Wpointer-arith -mthreads -mtune=pentiumpro -O3 -g -pipe -pipe -I/cygdrive/c/ACE-5.
7.0/ACE_wrappers -DACE_HAS_EXCEPTIONS -DACE_NO_INLINE -I.. -Wl,--enable-auto-import -Wl,-E
-L/cygdrive/c/ACE-5.7.0/ACE_wrappers/lib -L. -L../lib -o sThreadPool.exe .obj/sThreadPool.o
-lACE
make[1]: Leaving directory `/cygdrive/c/ACE-5.7.0/ACE_wrappers/tests/SThreadPool'

[email protected] /cygdrive/c/ACE-5.7.0/ACE_wrappers/tests/SThreadPool
$ ./sThreadPool.exe

The program works fine when I comment the object creation. 

/*HA_CommandHandler handler;
handler.activate( THR_NEW_LWP | THR_JOINABLE, 4);
create 4 threads
handler.wait();*/

------------------------------------------------------------------------

$ make
make[1]: Entering directory `/cygdrive/c/ACE-5.7.0/ACE_wrappers/tests/SThreadPool'

GNUmakefile: /cygdrive/c/ACE-5.7.0/ACE_wrappers/tests/SThreadPool/GNUmakefile.sThreadPool MA
KEFLAGS=w

g++ -Wpointer-arith -mthreads -mtune=pentiumpro -O3 -g -pipe -pipe -I/cygdrive/c/ACE-5.
7.0/ACE_wrappers -DACE_HAS_EXCEPTIONS -DACE_NO_INLINE -I.. -c -o .obj/sThreadPool.o sThread
Pool.cpp
g++ -Wpointer-arith -mthreads -mtune=pentiumpro -O3 -g -pipe -pipe -I/cygdrive/c/ACE-5.
7.0/ACE_wrappers -DACE_HAS_EXCEPTIONS -DACE_NO_INLINE -I.. -Wl,--enable-auto-import -Wl,-E
-L/cygdrive/c/ACE-5.7.0/ACE_wrappers/lib -L. -L../lib -o sThreadPool.exe .obj/sThreadPool.o
-lACE
make[1]: Leaving directory `/cygdrive/c/ACE-5.7.0/ACE_wrappers/tests/SThreadPool'

[email protected] /cygdrive/c/ACE-5.7.0/ACE_wrappers/tests/SThreadPool
$ ./sThreadPool.exe
In Main
In Main

--------------------------------------------------------------------------


----------



## sathavale (Oct 28, 2009)

I can't think of a reason why this should break!! Can one of you help me out please?


----------

