Example #1
0
int main(void)
{
  
  int err1, err2;
  int i;
    
  if (mythread_barrier_init(&mybarrier, NULL, 10) != 0){
    printf("Barrier Test Fail\n");
    return 1;
  }



  for(i=0;i<10;i++){
    err1 = mythread_create(&threadc[i],NULL,barrierTest,NULL);
    if (err1 != 0) printf("Barrier Test Fail\n");
  }
    

  for(i=0;i<10;i++){
    err1 = mythread_join(threadc[i],NULL);
    if (err1 != 0) printf("Barrier Test Fail\n");
  }
  
  if (mythread_barrier_destroy(&mybarrier) != 0){
        printf("Barrier Test Fail\n");
        return 1;
    }

  printf("Barrier Test PASS\n");

  return 0;

}
Example #2
0
int main (void)
{
    int test;
    // initialize mutex, condition variable and barrier.   
    mythread_mutex_init (&mut, NULL);
    mythread_cond_init (&con, NULL);
    // 4 threads will be calling wait. 
    mythread_barrier_init (&barr, NULL, 4);

    mythread_setconcurrency (8);

    // create first 3 threads and assign them functions. 
    mythread_create (&t1, NULL, foo1, NULL);
    mythread_create (&t2, NULL, foo2, NULL);
    mythread_create (&t3, NULL, foo3, NULL);
    
    // stall main until all threads have finished. 
   if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD)) 
    
      PASS--;

   // after this stage, mutex and condition variables are not required, destroy them. 
   if ( mythread_cond_destroy (&con) != 0)
     PASS --;

   if ( mythread_mutex_destroy (&mut) != 0)
     PASS --;
  
    // create next 3 threads and assign functions. This is to test reusability of barriers. 
    mythread_create (&t4, NULL, foo3, NULL);
    mythread_create (&t5, NULL, foo4, NULL);
    mythread_create (&t6, NULL, foo5, NULL);
    
    // let main also wait on the barrier
   if ((test = mythread_barrier_wait (&barr) != 0) && (test != MYTHREAD_BARRIER_SERIAL_THREAD)) 
     PASS--;
 
    // at this point, we are sure that all the threads have finished running. Now check value of PASS for any errors. 
    // If PASS is 0, means all function executed successfully. If PASS is less than 0, atleast one function returned error. 
    // print PASS or FAIL accordingly. 

    if (PASS == 0)
      printf ("PASS\n");
    else
      printf ("FAIL\n");

    //destroy the barrier and exit. 
    mythread_barrier_destroy (&barr);      
    mythread_exit(NULL);
    return 0;
}
int main(int argc, char **argv)
{	
	int i;
	char buffer[1024];
	futex_init(&printFutex, 1);
	mythread_t mythread1[NTHREADS];
	mythread_t signalingThread[NTHREADS/2];
	mythread_setconcurrency(4);
	mythread_mutex_init(&mutex,NULL);
	mythread_cond_init(&condition,NULL);
	mythread_barrier_init(&barrier,NULL,NTHREADS+1);
	
	/* Create Threads */
	for(i=0;i<NTHREADS;i++){
		sprintf(buffer, "Created thread : %d\n", i+1);
		printToConsole(buffer);
		mythread_create(&mythread1[i],NULL,&thread_func, NULL);
	}
	
	/*Signal threads waiting on cond var*/
	while(count<NTHREADS){
		/* Special case for testing broadcast*/
		if(count == NTHREADS/2){
			mythread_cond_broadcast(&condition);
		}else{
			mythread_cond_signal(&condition);
		}
	}
	
	/* Waiting on barrier. Last thread, or this main thread will unblock the barrier depending on the execution sequence */
	mythread_barrier_wait(&barrier);
	sprintf(buffer, "Out of barrier, main thread exiting..\n");
	printToConsole(buffer);
	/* Destroy mutex, barrier and cond*/
	mythread_cond_destroy(&condition);
        mythread_mutex_destroy(&mutex);
	mythread_barrier_destroy(&barrier);
	sprintf(buffer, "Finished Execution\n");
	printToConsole(buffer);
}
Example #4
0
int main()
{
  pthread_t thread[N];
  //Sid: Test1: Test with first barrier
  mythread_barrier_init(&barrier,NULL,N);
  int i = 0;
  for ( i = 0; i <N; i++) {
     pthread_create(&thread[i], NULL, &barrierTest, NULL);

  }
  for ( i = 0; i<N; i++) {
     pthread_join(thread[i], NULL);
  }
  if ( first_test == N && second_test == N) {
          printf("PASS \n");
       }else{
          printf("FAIL \n");
       }

  mythread_barrier_destroy(&barrier);
  return 1;
}