int thread_init() { /* Allocate space MAX_THREADS threads. */ cb_table = calloc(MAX_THREADS, sizeof(thread_control_block*)); sem_table = calloc(MAX_SEM, sizeof(sem_t*)); run_queue = queue_create(); set_quantum_size(10000); return 0; }
int main() { getcontext(&uctx_main); //AMJAD: Added uctx_main int thread_num = 10; int j; char* thread_names[] = { "thread 0", "thread 1", "thread 2", "thread 3", "thread 4", "thread 5", "thread 6", "thread 7", "thread 8", "thread 9" }; /* Initialize MyThreads library. */ mythread_init(); /* 250 ms */ set_quantum_size(250); counter_mutex = create_semaphore(1); for(j=0; j<thread_num; j++) { mythread_create(thread_names[j], (void *) &handler, 6004); } /* Print threads informations before run */ mythread_state(); /* When this function returns, all threads should have exited. */ runthreads(); destroy_semaphore(counter_mutex); /* Print threads informations after run */ mythread_state(); printf("The counter is %d\n", counter); printf("The result is %f\n", result); if (counter == 50 && (result - 151402.656521) < 0.000001) printf(">>> Thread library PASSED the Test 1\n"); exit(0); }
int main() { int thread_num = 10; int j; char* thread_names[] = { "thread 0", "thread 1", "thread 2", "thread 3", "thread 4", "thread 5", "thread 6", "thread 7", "thread 8", "thread 9" }; /* Initialize MyThreads library. */ init_my_threads(); /* 250 ms */ set_quantum_size(250); counter_mutex = create_semaphore(1); for(j=0; j<thread_num; j++) { create_my_thread(thread_names[j], (void *) &handler, 64000); } /* Print threads informations before run */ my_threads_state(); /* When this function returns, all threads should have exited. */ runthreads(); destroy_semaphore(counter_mutex); /* Print threads informations after run */ my_threads_state(); printf("The counter is %d\n", counter); printf("The result is %f\n", result); exit(0); }