コード例 #1
0
ファイル: mytest.c プロジェクト: vinothsid/os-mutex
void *thread1() {

//	printOut(itoa(mythread_self(),10));
//	printOut("Thread1\n");
	int i=0;
	while(i<200) {
  		mythread_mutex_lock(&lock);
		glval++;
		//usleep(100);
                if(glval==100) {
                        printOut("Thread1 sleeping for 10000 microseconds to hit blocking mutex condition in thread2\n");
			mythread_cond_signal(&cond);
			mythread_cond_broadcast(&cond1);

                        usleep(10000);

                }

		mythread_mutex_unlock(&lock);
		printOut("Using IO in thread1 \n");
		i++;
	}

	
}
コード例 #2
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);
}