void tree(void* p) { int x = (int)p; int i; // msg for (i=0; i<x; i++) fputs(" ", stderr); fprintf(stderr, "Starting %p\n", mythread_myid()); // spawn child if (x <3) { for (i=0; i<x; i++) fputs(" ", stderr); fprintf(stderr, "About to spawn child %p\n", mythread_myid()); ThreadId tid; mythread_create(&tid, NULL, tree, (void*)(x+1)); // wait for child ThreadId child = mythread_join(tid); for (i=0; i<x; i++) fputs(" ", stderr); fprintf(stderr, "Joined with child(%p) in %p\n", child, mythread_myid()); } for (i=0; i<x; i++) fputs(" ", stderr); fprintf(stderr, "Exiting %p\n", mythread_myid()); mythread_exit(); }
int main() { int i = 0; mythread_t tid[10]; void *status; // initialize key mythread_key_create(&name_key, free); mythread_key_create(&id_key, free); for (i = 0; i < 10; i++) { int *temp = (int *) (malloc) (sizeof(int)); *temp = i; mythread_create(&tid[i], NULL, hello, (void *) temp); mythread_yield(); } printf("main thread is joining with child threads\n"); for (i = 0; i < 10; i++) { mythread_join(tid[i], &status); printf("joined on thread:%d\n", tid[i]); } mythread_key_delete(id_key); mythread_key_delete(name_key); printf("main thread is exiting\n"); fflush(NULL); mythread_exit(NULL); return 0; }
int main() { int i; mythread_t thread[NR_THREADS]; mythread_setconcurrency(3); mythread_mutex_init(&mutex, NULL); mythread_cond_init(&okToProceed, NULL); for (i = 0; i < NR_THREADS; i++) mythread_create(&thread[i], NULL, runner, NULL); sleep(2); printf("Main: sending broadcast signal to all threads\n"); fflush(stdout); mythread_cond_broadcast(&okToProceed); printf("Main: joining all threads\n"); fflush(stdout); for (i = 0; i < NR_THREADS; i++) mythread_join(thread[i], NULL); printf("Main: about to exit\n"); fflush(stdout); mythread_exit(NULL); mythread_cond_destroy(&okToProceed); mythread_mutex_destroy(&mutex); return 1; }
void MyExampleThread1(int* arg) { std::cout << "B" << std::endl; mythread_yield(); std::cout << "E" << std::endl; mythread_exit(); }
void MyExampleThread2(int* arg) { std::cout << "D" << std::endl; mythread_yield(); std::cout << "H" << std::endl; mythread_exit(); }int main() {
void worker(void* p) { int x = (int)p; fprintf(stderr, " in worker: %p (%d)\n", mythread_myid(), x); mythread_exit(); }
void worker1(void* p) { mythread_detach(); mythread_semwait(&mutex); collected++; mythread_sempost(&mutex, 1); mythread_exit(); }
void* test(void* arg){ printf("This is the %d test thread\n", *(int *)arg); int i; for(i =0; i<500;i++){ printf("thread1 %d\n", i); int j; for(j = 0; j<1000000;j++); } mythread_exit(1); }
static void thread_function1(void *args) { int i; mutex_lock(&out_mutex); for(i=0;i<10;i++) { printf("Hello %d from thread one\n", i); usleep(500000); } mutex_unlock(&out_mutex); mythread_kill(1002); mythread_exit(); }
void *fun(void *arg) { int i; mythread_mutex_lock(&mut); printf("Will now wait\n"); fflush(stdout); mythread_cond_wait(&cond, &mut); for(i = 0; i < INT_MAX/10; i++); printf("Exiting \n"); fflush(stdout); mythread_mutex_unlock(&mut); mythread_exit(NULL); return NULL; }
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; }
void *runner(void *arg) { int i; mythread_mutex_lock(&mutex); printf("Thread:Waiting for main thread to broadcast\n"); fflush(stdout); mythread_cond_wait(&okToProceed, &mutex); sleep(0.2); printf("Thread:Out of conditonal wait and about to exit\n"); fflush(stdout); mythread_mutex_unlock(&mutex); mythread_exit(NULL); return NULL; }
void checknull(void* p) { int x = (int)p; int i; for (i=0; i<x; i++) { ThreadId tid; mythread_create(&tid, NULL, worker, (void*)(i)); } for (i=0; i<x; i++) { ThreadId child = mythread_join(NULL); fprintf(stderr, "joined with %p\n", child); } mythread_exit(); }
int main(int argc, char **argv) { mythread_t thread[MAX_THREADS]; int rc=0, i; struct threadValues *value; for(i=0; i < MAX_KEYS; i++) { rc = mythread_key_create(&keys[i], NULL); if(rc != 0) write(1,"\nmythread_key_create() failed\n",29); } printf("Create threads\n"); for (i=0; i < MAX_THREADS; ++i) { value = (struct threadValues *)malloc(sizeof(struct threadValues)); value->v1 = i; value->v2 = i+10; rc = mythread_create(&thread[i], NULL, thread_function, value); if(rc != 0) printf("\nmythread_create() call error"); } printf("Wait for half the threads to complete\n"); for (i=0; i < MAX_THREADS; ++i) { rc = mythread_join(thread[i], NULL); if(rc != 0) printf("\nmythread_join() call error\n", rc); } //delete half the keys for(i=0; i < MAX_KEYS; i++) { rc = mythread_key_delete(keys[i]); if(rc != 0) printf("\nmythread_delete() call error\n", rc); } printf("Main before exiting\n"); mythread_exit(0); //delete other half of the keys for(i=MAX_KEYS/2; i < MAX_KEYS; i++) mythread_key_delete(keys[i]); printf("Main after exiting\n"); }
void *barrierTest(void* arg){ mythread_enter_kernel(); printf("Going to sleep...%ld\n",mythread_self()); mythread_leave_kernel(); //sleep(5); mythread_barrier_wait(&mybarrier); mythread_enter_kernel(); printf("Moving towards new barrier... %ld\n",mythread_self()); mythread_leave_kernel(); mythread_barrier_wait(&mybarrier); mythread_enter_kernel(); printf("%ld exiting... %ld\n",mythread_self()); mythread_leave_kernel(); mythread_exit(NULL); }
void thread(void* p) { fputs("Entered\n", stderr); int x = (int)p; int y = 2; fprintf(stderr, "Entered thread %d with %d\n", (int)mythread_myid(), x); mythread_yield(); fprintf(stderr, "back in thread %d with %d and %d\n", (int)mythread_myid(), x, y++); mythread_yield(); fprintf(stderr, "back in thread %d with %d and %d\n", (int)mythread_myid(), x, y++); if (left) { ThreadId tid3; left--; mythread_create(&tid3, NULL, thread, (void*)left); } mythread_exit(); }
/* Each thread will increment the count (global) protected by mutex */ void* thread_func(void *arg) { char buffer[1024]; /*Lock Mutex*/ int temp = 0; mythread_mutex_lock(&mutex); while(temp<1000){ temp++; } /*Wait to increment until main thread signals*/ mythread_cond_wait(&condition,&mutex); count++; mythread_enter_kernel(); sprintf(buffer, "Incremented Count to : %d\n", count); printToConsole(buffer); mythread_leave_kernel(); mythread_mutex_unlock(&mutex); /* Threads will wait on the barrier. Main thread will also wait */ mythread_barrier_wait(&barrier); mythread_exit(NULL); }
void handler() { int i; for(i=0; i < 5; i++) { /* If you remove this protection, you should be able to see different * out of every time you run this program. With this protection, you * should always be able to see result to be 151402.656521 */ semaphore_wait(counter_mutex); /* down semaphore */ /* START CRITICAL REGION */ int j; for (j = 0; j < 1000; j++) { result = result + sin(counter) * tan(counter); } counter++; /* END CRITICAL REGION */ semaphore_signal(counter_mutex); /* up semaphore */ } mythread_exit(); /* exit thread */ }
void checkcollect(void* p) { int nt = (int)p; spawned = 0; collected = 0; mythread_seminit(&mutex, 1); /* mutex = 1 */ int i; for (i=0; i<nt; i++) { ThreadId tid1; mythread_create(&tid1, NULL, worker1, 0); spawned++; mythread_yield(); } while ((collected < spawned)) { printf("%d spawned, %d collected\n", spawned, collected); mythread_yield(); } printf("%d spawned, %d collected\n", spawned, collected); mythread_exit(); }
int main() { int i; mythread_t thread[NTHREADS]; mythread_setconcurrency(NTHREADS+1); mythread_mutex_init(&mut, NULL); mythread_cond_init(&cond, NULL); for(i = 0; i < NTHREADS; i++) mythread_create(&thread[i], NULL, fun, NULL); for(i = 0; i < INT_MAX/10; i++); printf("Broadcast\n"); fflush(stdout); mythread_cond_broadcast(&cond); printf("Now join\n"); fflush(stdout); for(i = 0; i < NTHREADS; i++) mythread_join(thread[i], NULL); printf("main exit\n"); fflush(stdout); mythread_exit(NULL); mythread_cond_destroy(&cond); mythread_mutex_destroy(&mut); return 1; }
void* test2(void* arg){ printf("This is the %d test thread\n", *(int *)arg); sleep(5); mythread_exit(1); }
void *exitThread() { printOut("tests mythread_exit():calling mythread_exit\n"); mythread_exit(NULL); printOut("This should not be printed\n"); }
void run_function(int thread_id, void (*func)(int *), int *args){ this_th_th = thread_id; func(args); mythread_exit(); }
int main() { mythread_setconcurrency(4); mythread_mutex_init(&lock,NULL); mythread_mutex_init(&lock1,NULL); mythread_cond_init(&cond,NULL); mythread_cond_init(&cond1,NULL); glval=0; glval1=0; mythread_t tid1,tid2,tid3,tid4,tid5; mythread_queue_t head; tid1 = malloc(sizeof(struct mythread)); tid2 = malloc(sizeof(struct mythread)); tid3 = malloc(sizeof(struct mythread)); tid4 = malloc(sizeof(struct mythread)); tid5 = malloc(sizeof(struct mythread)); /**************** Test cases for testing all functions except destroy functions ********************/ mythread_create(&tid3,NULL,thread3,NULL); //Thread3 going to call condition wait and will be woken up(condition signal) mythread_create(&tid4,NULL,thread4,NULL); //Thread4 going to call condition wait and will be woken up(condition broadcast signal) mythread_create(&tid5,NULL,thread5,NULL); //Thread5 going to call condition wait and will be woken up(condition broadcast signal) printOut("Sleeping for 1 second so that threads calling condition waits starts before their condition variables are signaled\n"); sleep(1); // Sleep for 1 second so that tid3,tid4,tid5 waits on condition wait before condition signal/broadcast is sent mythread_create(&tid1,NULL,thread1,NULL); mythread_create(&tid2,NULL,thread2,NULL); mythread_join(tid1,NULL); mythread_join(tid2,NULL); printOut("Value of global variable incremented 200 times in two threads : "); printOut(itoa(glval,10)); printOut("\n"); /*************************************************************************************************/ mythread_join(tid3,NULL); mythread_join(tid4,NULL); mythread_join(tid5,NULL); printOut("=================================================================\n"); printOut("Repeat the entire test after destroying(mythread_mutex_destroy,mythread_cond_destroy) and reinitializing 2 locks and 2 condition variables\n"); printOut("=================================================================\n"); /********* Destroying all mmutexes and locks and reinitialising again ****************************/ mythread_mutex_destroy(&lock); mythread_mutex_destroy(&lock1); mythread_cond_destroy(&cond); mythread_cond_destroy(&cond1); mythread_mutex_init(&lock,NULL); mythread_mutex_init(&lock1,NULL); mythread_cond_init(&cond,NULL); mythread_cond_init(&cond1,NULL); /**************************************************************************************************/ /**************** Test cases for testing all functions except destroy functions ********************/ glval=0; mythread_create(&tid3,NULL,thread3,NULL); //Thread3 going to call condition wait and will be woken up(condition signal) mythread_create(&tid4,NULL,thread4,NULL); //Thread4 going to call condition wait and will be woken up(condition broadcast signal) mythread_create(&tid5,NULL,thread5,NULL); //Thread5 going to call condition wait and will be woken up(condition broadcast signal) printOut("Sleeping for 1 second so that threads calling 'condition wait' starts before their condition variables are signaled\n"); sleep(1); // Sleep for 1 second so that tid3,tid4,tid5 waits on condition wait before condition signal/broadcast is sent mythread_create(&tid1,NULL,thread1,NULL); mythread_create(&tid2,NULL,thread2,NULL); mythread_join(tid1,NULL); mythread_join(tid2,NULL); printOut("Value of global variable incremented in the repeated test 200 times in two threads : "); printOut(itoa(glval,10)); printOut("\n"); /**************************************************************************************************/ mythread_exit(NULL); }