0
votes

I'm learning pthreads in c++. I've stucked at some point and searched on the web, however the same sentence exists in all pages, however not any explanation about this sentence. I couldn't understand the below sentence.

A joining thread can match one pthread_join() call. It is a logical error to attempt multiple joins on the same thread.

It is from this link : http://www.bogotobogo.com/cplusplus/multithreading_pthread.php

Could you explain what this sentence mean? Could you explain what the logical error is by an example?

What is "joining thread"? Say that, Main thread creates a child thread, main thread waits for child thread to complete its job. OK. in this case, which one is the joining thread , main thread or child thread?

2
Once a thread has been joined, it doesn't exist any more, so it would clearly be an error of trying to join the (now non-existing) thread again. - Some programmer dude

2 Answers

1
votes

For example, if main() thread joins on a thread twice.

 pthread_t t1;

 pthread_create(&t1, ....);
 pthread_join(t1, NULL); 
 pthread_join(t1, NULL); /* The quoted sentence refers to cases like this */

The same is applicable for any other thread.

1
votes

It's simple: you may only join a thread once.

                   main        worker 1
                     |
                     |
  pthread_create     +----------->+
                     |            |
                     |            |
  pthread_join       ⁞            |
        |            ⁞            | (done now)
        ┴            +<-----------+
                     |
                     |
                    (etc).