multithreading - How to declare a thread id in c? -
i trying write 2 codes. 1 mutex
, without mutex
. in program, have 2 threads. while declaring thread id have declared pthread_t tid[2];
not getting why value 2 here? have got sample code. if make value 1 still works , gives me correct value. if make pthread_t tid[0]
, not work. if put value 10000
, not getting right answer program. so, question value inside brace after tid
means? have provided code below:
#include<stdio.h> #include<string.h> #include<pthread.h> #include<stdlib.h> #include<unistd.h> pthread_t tid[10000]; int counter; void* dosomething(void *arg) { unsigned = 0; counter += 1; printf("\njob %d started\n", counter); for(i=0; i<(0xffffffff);i++); // printf(" \njob %d finished\n", counter); return null; } int main() { int = 0; int err; while(i < 2) { err = pthread_create(&(tid[i]), null, &dosomething, null); if (err != 0) printf("\ncan't create thread :[%s]", strerror(err)); i++; } //printf("value of pthread %d\n",err); pthread_join(tid[0], null); pthread_join(tid[1], null); return 0; }
Comments
Post a Comment