void *hello(void *t) { ///int *reps = (int*)t; int i = 0; // initialize thread specific data for each thread int* id = (int*)malloc(sizeof(int)); assert(id); *id = mythread_self(); mythread_setspecific(id_key, id); //set name char* name = (char*)malloc(sizeof(char)*MAX_NAME_LENGTH); assert(name); memset(name, '\0', MAX_NAME_LENGTH); sprintf(name, "worker-%d", mythread_self()); mythread_setspecific(name_key,name); // user specific data for (; i < mythread_self(); i++) { printf("Thread-%d:Hello thread:%d\n", mythread_self(), i); sleep(1); mythread_yield(); } printf("Thread specific data for %d thread is %d and worker name is %s\n", mythread_self(), *((int *)mythread_getspecific(id_key)), (char*)mythread_getspecific(name_key)); return NULL; }
void *thread_function(void *arg) { int rc, i; mythread_t thread,childthread ; struct threadValues *value; value = (struct threadValues *)arg; thread=mythread_self(); printf("This is thread %d before yielding\n",thread.tid); for(i=0; i < MAX_KEYS; i++) { printf("\nSettig values %d %d for key %d by thread %d", value->v1, value->v2, i, thread.tid); rc = mythread_setspecific(keys[i], value); if(rc != 0) { printf("\nmythread_setspecific call error for thread = %d", keys[i]); } } rc=mythread_create(&childthread,NULL,&printingfunction,NULL); rc=mythread_yield(); printf("this is thread %d after yield\n",thread.tid); //rc = mythread_yield(); getData(); return NULL; }