I created threads just fine. However, when I try to loop the main program and execute the threads and the functions anew all over again, the thread creation does not complete. It is weird because I don't get any error message. It just creates the first set of threads, once it is finished and tries to loop to create new threads all over again, it does not. My guess is that the threads are already created and it cannot reuse them. I called cancel thread on the cleanup section of my code, but it did not recognize the thread passed in. What am I doing wrong?

Code:
//------------------------ Methods ---------------------------------
int main ( int argc, char ** args )
{
  if ( !parseArgs(argc, args) )
    {
      fprintf(stderr,"Usage: %s -n numLoops -i userPercentage -p numLists -s queueSize\n", args[0]);
      return 1;
    }

	while(numLoops>0)
	{
		printf("\nNUMLOO{s : %d",numLoops);
		printf("userper: %dl",userPer);
printf("\nnumlist: %d",numList);
printf("\nqzises: %d", qSize);
  initialize(); // initialize variables
  generateProducerThreads();
  generateConsumerThreads();
  mainThreadWait();
      cleanup(); // free memory
  numLoops--;
}

  return 0;
}




void mainThreadWait()
{
  int totalItems = numCP * numList;
  int i;

  // continuously check if the total number of items to be produced has been
  // consumed;
  while ( true )
    {
      if ( consumedCount == totalItems )
	{
	  // all done, cancel all threads, exit program
	  for ( i=0; i < numCP; i++ )
	    {
	      if ( pthread_cancel(bsConsIDs[i]) != 0 )
		fprintf(stderr,"pthread_cancel failed"), exit(1);
	    }

	  printf("MAIN THREAD: All items consumed, exiting program.\n");
	  return;
	}
      else // sleep for a second
	{
	  sleep(1);
	}
    }
}

void initialize()
{
  initoutput(&output); //->>>>>**changed to take in ptr
  queueInit(qSize);
  Init(&MUTEX_SEM, 1);
  Init(&EMPTY_SEM, qSize);
  Init(&FULL_SEM, 0);
  Init(&ITEM_ID_MUTEX_SEM, 1);
  Init(&INCR_CONS_COUNT_SEM, 1);
    Init(&DISPLAY_FROM_UI, 1); //->>>>>>>**new init
}



void cleanup()
{
	Remove(MUTEX_SEM);
  Remove(EMPTY_SEM);
  Remove(FULL_SEM);
  Remove(ITEM_ID_MUTEX_SEM);
  Remove(INCR_CONS_COUNT_SEM);
  	Remove(DISPLAY_FROM_UI); //->>>>**new remove
  queueDestroy();
  free(prodIDs);
  free(bsConsIDs);
    free(userIDs); //->>>*new free
    int i;
    	  for ( i=0; i < 2; i++ )
		    {
				printf("\n\nI woudlbe %d\n\n",i);
		      if ( pthread_cancel(prodIDs[i]) != 0 )
			fprintf(stderr,"\n the new thread cancel failed"), exit(1);
	    }
  close(output);
}



void generateProducerThreads()
{
	printf("\nIn producer threads\n");
  int i=0;

  // allocate prodIDs array
  if ( (prodIDs=malloc(numCP * sizeof(int))) == NULL )
    fprintf(stderr,"malloc failed (out of memory)\n"), exit(1);
printf("\n\nPRIDIS: %d",prodIDs);

  for ( i=0; i < numCP; i++ )
    {
		printf("\nABOUT TO craete rorpd thrd\n");
      if ( pthread_create(prodIDs+i,
			  NULL,
			  (void*)(&startProducer),
			  prodIDs+i) != 0 )
	fprintf(stderr,"pthread_create failed\n"), exit(1);
	printf("\nFINISHE MAKING THRED\n");
    }
    printf("\nDONE IN PROD THREAD\n");
}



void generateConsumerThreads()
{
  int i=0;

  // allocate bsConsIDs array
  if ( (bsConsIDs=malloc(numCP * sizeof(int))) == NULL )
    fprintf(stderr,"malloc failed (out of memory)\n"), exit(1);
    printf("\n\nCONSIDS: %d",bsConsIDs);
  for ( i=0; i < numCP; i++ )
    {
      // bubble sort consumer
      if ( pthread_create(bsConsIDs+i,
			  NULL,
			  (void*)(&startBubSortConsumer),
			  bsConsIDs+i) !=0 )
	fprintf(stderr,"pthread_create failed\n"), exit(1);
    }
}