/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// This example is from the ACE Programmers Guide.
////  Chapter:  "Thread Management"
//// For details please see the guide at
//// http://www.cs.wustl.edu/~schmidt/ACE.html
////  AUTHOR: Umar Syyid (usyyid@hns.com)
//// and Ambreen Ilyas (ambreen@bitsmart.com)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Example 5
#include "ace/OS.h"
#include "ace/Synch.h"
#include "ace/Synch_T.h"
#include "ace/Token.h"

//Arguments that are to be passed to the worker thread are passed through this class.
class Args{
public:
Args(int iterations):
 mutex_(),iterations_(iterations){}
ACE_Token mutex_;
int iterations_;
};

//The starting point for the worker threads
static void*
worker(void*arguments){
Args *arg= (Args*) arguments;
for(int i=0;i<arg->iterations_;i++){
 ACE_DEBUG((LM_DEBUG,"(%t) Trying to get a hold of this iteration\n"));
  {//begin critical section
  ACE_Guard<ACE_Token> guard(arg->mutex_);
    //This is our critical section
  ACE_DEBUG((LM_DEBUG,"(%t) This is iteration number %d\n",i));
  //work
  ACE_OS::sleep(2);
  }//end critical section
 }
return 0;
}
 

int main(int argc, char*argv[]){
if(argc<3){
 ACE_OS::printf("Usage: egx <number_of_threads> <number_of_iterations> \n");
 ACE_OS::exit(1);
 }

//Setup the arguments
Args arg(ACE_OS::atoi(argv[2]));
//Spawn the worker threads
ACE_Thread::spawn_n(ACE_OS::atoi(argv[1]),(ACE_THR_FUNC)worker,(void*)&arg);
while(ACE_Thread::join(NULL,NULL,NULL)==0);
}

 Next Example