Questions about POSIX threads in C

#include <pthread.h> #define NUM_THREADS 4 void *work(void *i){ printf("Hello, world from %i\n", pthread_self()); pthread_exit(NULL); } int main(int argc, char **argv){ int i; pthread_t id[NUM_THREADS]; for(i = 0; i < NUM_THREADS; ++i){ if(pthread_create(&id[i], NULL, work, NULL)){ printf("Error creating the thread\n"); exit(19); } } printf("After creating the thread. My id is: %i\n", pthread_self()); return 0; } 

I know the result:

 Hello, world from 2 Hello, world from 3 After creating the thread. My id is: 1 Hello, world from … 

First of all, this is not homework. POSIX is not my area, so I just want to explain the result (there is no need to explain the functions used, since I know what they do) some quick answers:

  • Are the identifiers for pthreads (2, 3, 1) given by the system?
  • used ++i ... did it somehow affect the result?
  • Why at the end there are only 4 threads (3 + main), why not 5 ?!
  • why is it after the main print After creating .... another thread executed ?? !! how come ?? ??
+4
source share
4 answers
  • I'm not sure if this is indicated, but I believe (almost?) All thread IDs start at 1 (main) and increment from there.
  • No, ++i has nothing to do with the output.
  • Since you did not make pthread_join threads, the main function (and therefore the program) exited before the last thread was executed.
  • I'm not sure exactly what you are trying to ask here, but the answer is concurrency. There is no guarantee when or in what order threads are executed.
+4
source
  • They are thread identifiers within the current process.
  • No, I do not affect the output, because you do not pass it to the threads as a parameter or do not access it from the stream code. So this is just a loop variable.
  • I do not know how many outputs there are, because you are not giving us full information. I agree that usually there should be 5 threads. It is possible that some threads do not have time to print their message before the main thread exits. You must join them before exiting the main thread.
  for(i = 0; i < NUM_THREADS; i++) { pthread_join(id[i], NULL); } 

4.Threads run parallel to the main thread. Therefore, the code of each of the threads that you run is executed simultaneously with the code of the main thread after they are launched. This means that the main thread continues to execute printf while the child threads do their work, so the output is alternated "randomly".

+1
source

3 and 4: threads are asynchronous: with pthreads, when you exit the main function, all threads terminate regardless of whether they ended or not.

Thus, only 3 threads were printed before main left the loop, 1 was printed between the main print statement and return, and one thread did not get that far.

+1
source

pthread_self does not return a numeric type, but an opaque type pthread_t (which may be an opaque struct ).

I suggest you clear the array with

  memset(id, 0, sizeof(id)); 

Indeed, in GNU / Linux / Debian / Sid / x86-64, the internal include /usr/include/x86_64-linux-gnu/bits/pthreadtypes.h file has

  typedef unsigned long int pthread_t; 

But I don’t think you should believe that this is a thread identification, just an opaque descriptor (like file descriptors).

I suggest you read a good pthreads tutorial, for example, for example. this blaze barney

Currently, threads of multi-core machines [can] work differently on different cores.

0
source

Source: https://habr.com/ru/post/1389935/


All Articles