static void waitForThreads() {
    // Wait for all threads to be created.
    while (NumOfThreads() != ROLE_SIZE) {
        DoYield();
    }
    
    // Wait for the ROLE_CANCELED thread to be ready for cancelation
    while (!readyToCancel) {
        DoYield();
    }
    
    // Send a cancel request to the ROLE_CANCELED thread.
    CancelThread(threads[ROLE_CANCELED]);
    
    // Wait for all threads that are supposed to exit.
    WaitForThread(threads[ROLE_FINISH]);
    WaitForThread(threads[ROLE_EXIT]);
    
    // The timing of the effect of pthread_cancel is undefined, this could cause the aplication to hang.
    // Currently commented out.
    WaitForThread(threads[ROLE_CANCELED]); 
}
Example #2
0
 void Stop()
 {
     CancelThread(fThread);
 }
Example #3
0
int gxThread::DestroyThreadPool(thrPool *thread_pool, int check_state)
// Destroy a previously created thread pool. If the "check_state" variable
// is true the current state of each thread will be checked before it is
// destroyed. If a thread cannot be canceled and the "check_state" variable
// is true, this function will return a non-zero value.
{
  if(!thread_pool) return 1;

  thrPoolNode *ptr;
  gxThread_t *thread = 0;
  gxThreadType type;
  gxThreadState state;
  
  while(!thread_pool->IsEmpty()) {
    // Start checking at the tail since the higher priority threads
    // at the head of the pool would most likely have exited first.
    ptr = thread_pool->GetTail();
    state = ptr->GetThreadPtr()->GetThreadState();
    type = ptr->GetThreadPtr()->GetThreadType();
    switch(state) {
      case gxTHREAD_STATE_INVALID :
	// We don't know what state this thread is in so lets delete it
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
      case gxTHREAD_STATE_CANCELED :
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
      case gxTHREAD_STATE_EXITED :
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
      case gxTHREAD_STATE_NEW :
	// Newly created thread that was never executed
	// Lets assume that is not intended to be executed
	// at this point.
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
      case gxTHREAD_STATE_RUNNING :
	if((check_state) && (type != gxTHREAD_TYPE_DETACHED)) {
	  // PC-lint 09/14/2005: thread may not be initalized 
	  thread = ptr->GetThreadPtr();
	  if(thread) {
	    if(CancelThread(thread) != 0) return 1;
	  }
	}
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
      case gxTHREAD_STATE_SUSPENDED :
	if((check_state) && (type != gxTHREAD_TYPE_DETACHED)) {
	  // PC-lint 09/14/2005: thread may not be initalized 
	  thread = ptr->GetThreadPtr();
	  if(thread) {
	    if(CancelThread(thread) != 0) return 1;
	  }
	}
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
      case gxTHREAD_STATE_WAITING :
	if((check_state) && (type != gxTHREAD_TYPE_DETACHED)) {
	  // PC-lint 09/14/2005: thread may not be initalized 
	  thread = ptr->GetThreadPtr();
	  if(thread) {
	    if(CancelThread(thread) != 0) return 1;
	  }
	}
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
      default:
	thread = thread_pool->RemoveNode(ptr);
	if(thread) DestroyThread(thread, 0);
	break;
    }
  }

  delete thread_pool;
  thread_pool = 0;
  return 0;
}