c - What's the difference using only one pthread_t variables and multiple pthread_t variables? -


i want know differences of using different number of pthread_t variables.
here simple code made :

#include <stdio.h> #include <pthread.h>  void *thread(void *vargp);  int main(int argc, char **argv) {      pthread_t tid;       (int = 0; < 5; i++){           int* = malloc(sizeof(int));           *a = i;           pthread_create(&tid, null, thread, a);      }       if(pthread_join(tid, null) == -1){           printf("error");           exit(1);      } }  void *thread(void *vargp) {      int count = 1;      for(int = 0; <3; i++){           printf("count : %d, value : %d\n", count, (*(int *)vargp));           count++;      } } 

it works mean to. however, thought strange create 5 threads using 1 pthread_t variable !.. saw examples using same number of pthread variables number of thread create. example, if gonna create 5 threads below, have create 5 length pthread_t array, pthread_t tid[5] . tell me what's differences? thanks

pthread_t variable passed pthread_create thread identifier , can reused please. there's nothing wrong way used same idenitfier tid create multiple threads. you'll lose identity of other threads except last 1 created using tid continuously overwritten in loop.

however, if receive exit value thread (using pthread_join) or other operations such detaching (using pthread_detach -- create thread in detached state or thread can detach it), set/get scheduling parameters (using pthread_setschedparam, pthread_getschedparam) etc, main thread can't since don't have thread's id.

in short, code fine , you'll need store ids (such using array tid[5] in example) if want manipulate threads outside (as opposed within thread -- thread can id using pthread_self()).


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -