/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// 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 ([email protected])
//// and Ambreen Ilyas ([email protected])
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Example 8
#include "ace/Synch_T.h"
#include "ace/Synch.h"

ACE_Atomic_Op<ACE_Thread_Mutex,int> foo;

static void*
worker(void *arg){
  ACE_UNUSED_ARG(arg);
  foo=5;
  ACE_ASSERT (foo == 5);
 
  ++foo;
  ACE_ASSERT (foo == 6);
 
  --foo;
  ACE_ASSERT (foo == 5);
 
  foo += 10;
  ACE_ASSERT (foo == 15);
 
  foo -= 10;
  ACE_ASSERT (foo == 5);
 
  foo = 5L;
  ACE_ASSERT (foo == 5);
 return 0;
}

int main(int argc, char *argv[]){
if(argc<2){
 ACE_DEBUG((LM_DEBUG,"Usage: <program_name> <number of threads>\n"));
 ACE_OS::exit(1);
 }
 
int n_threads=ACE_OS::atoi(argv[1]);
ACE_DEBUG((LM_DEBUG,"Preparing to spawn %d threads\n",n_threads));
 

//Spawn off n_threads number of threads
for(int i=0; i<n_threads; i++){
 if(ACE_Thread::spawn((ACE_THR_FUNC)worker,0,THR_DETACHED|THR_NEW_LWP)==-1)
  ACE_DEBUG((LM_DEBUG,"Error in spawning thread\n"));
 }

//Wait for all the other threads to let the main thread know when it is time to exit
while(ACE_Thread::join(NULL,NULL,NULL)==0);
ACE_DEBUG((LM_DEBUG,"(%t)Other threads are finished. Program exiting..\n"));
}

 Next Example