int main() { int i; gtthread_t t1, t2, t3, t6; void *ret; gtthread_mutex_init(&count_lock); gtthread_init(5); gtthread_create(&t1, thr1, (void *) 1); gtthread_create(&t2, thr2, (void *) 2); gtthread_create(&t3, thr3, (void *) 3); gtthread_create(&t6, thr6, (void *) 6); for (i = 0; i < LOOP_MAX; i++) { printf("Main thread!\n"); fflush(stdout); gtthread_mutex_lock(&count_lock); count++; gtthread_mutex_unlock(&count_lock); if (i % YIELD_VAL == 0) { gtthread_yield(); } } printf("main waiting for thr1...\n"); fflush(stdout); gtthread_join(t1, &ret); printf("thr1 joined main with value %d!\n", (int) ret); fflush(stdout); gtthread_cancel(t2); printf("thr2 cancelled!\n"); fflush(stdout); printf("main waiting for thr3...\n"); fflush(stdout); gtthread_join(t3, &ret); printf("thr3 joined main with value %d!\n", (int) ret); fflush(stdout); printf("main waiting for thr6...\n"); fflush(stdout); gtthread_join(t6, &ret); printf("thr6 joined main with value %d!\n", (int) ret); fflush(stdout); gtthread_mutex_lock(&count_lock); printf("Final count value: %lu\n", count); fflush(stdout); gtthread_mutex_unlock(&count_lock); gtthread_mutex_destroy(&count_lock); return EXIT_SUCCESS; }
int main() { gtthread_t t1; gtthread_t t2; gtthread_t t3; double sum_thread1 = 0; double *p1 = &sum_thread1; double **p2 = &p1; gtthread_mutex_init(&g_mutex); gtthread_init(10000); gtthread_create(&t1, thr1, NULL ); gtthread_create(&t2, thr2, NULL ); gtthread_create(&t3, thr3, NULL ); printf("id of thread1 is %d\n", t1.id); printf("id of thread1 is %d\n", t2.id); // get self id gtthread_t t0; t0 = gtthread_self(); printf("The main thread is %d \n", t0.id); //gtthread_yield(); //gtthread_cancel(t2); gtthread_join(t1, (void **) p2); printf("Returned Value from thread 1 = %f\n", **p2); gtthread_join(t2, NULL ); gtthread_join(t3, NULL ); printf("returning to main thread!\n"); // printf("returning to main thread again!\n"); gtthread_mutex_destroy(&g_mutex); printf("\n....Exiting the Main!....\n"); return EXIT_SUCCESS; }
int main() { int rc; int rep; int ndx; printf("test mutex init, lock, unlock, destroy\n"); for( ndx=0; ndx<100; ++ndx ) { hold[ndx] = 100+(ndx%10)*10; } printf("main: enter\n"); gtthread_init(period); for( ndx=0; ndx<10; ++ndx ) { rc = gtthread_mutex_init(&m[ndx]); printf("init %d\n",ndx); fflush(stdout); if(rc)printf("rc = %d\n",rc); fflush(stdout); } for( rep=0; rep<10; ++rep ) { for( ndx=0; ndx<10; ++ndx ) { rc = gtthread_mutex_lock(&m[ndx]); printf("lock(%d)\n",ndx); fflush(stdout); if(rc)printf("rc = %d\n",rc); fflush(stdout); usleep(10*1000); rc = gtthread_mutex_unlock(&m[ndx]); printf("unlock(%d)\n",ndx); fflush(stdout); if(rc)printf("rc = %d\n",rc); fflush(stdout); } } for( ndx=0; ndx<10; ++ndx ) { rc = gtthread_mutex_destroy(&m[ndx]); printf("destroy(%d)\n",ndx,rc); fflush(stdout); if(rc)printf("rc = %d\n",rc); fflush(stdout); } printf("main: exit\n"); fflush(stdout); return EXIT_SUCCESS; }
int main() { gtthread_t t1; gtthread_t t2; gtthread_t t3; double sum_thread1 = 0; double *p1 = &sum_thread1; double **p2 = &p1; gtthread_mutex_init(&g_mutex); gtthread_init(1000); gtthread_create(&t1, thr1, NULL ); printf("thread1 id %ld\n", t1); //t1.id); gtthread_create(&t2, thr2, NULL ); printf("thread2 id %ld\n", t2); //t2.id); gtthread_create(&t3, thr3, NULL ); printf("thread3 id %ld\n", t3); //t2.id); // get self id gtthread_t t0; t0 = gtthread_self(); printf("main thread id %ld \n", t0); //t0.id); //gtthread_yield(); gtthread_cancel(t2); //gtthread_join(t1, NULL); gtthread_join(t1, p2); printf("thread1 return = %f\n", **p2); gtthread_join(t2, NULL ); gtthread_join(t3, NULL ); printf("returning to main thread!\n"); // printf("returning to main thread again!\n"); gtthread_mutex_destroy(&g_mutex); printf("\n....Exiting the Main!....\n"); return EXIT_SUCCESS; }