0

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?

1
  • 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. Commented Mar 1, 2015 at 16:40

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Ok. If more than one thread tries to join the same thread, what happens, is it ok?
It's undefined behaviour to attempt to join more than once with the same thread. If you do so, the program is invalid henceforth.
@oiyio: You're the one who quoted, in the question, that it's a logical error. That's what your whole question is about. So, no, it's not ok!!
Yes, but it's not "more than one thread" but "more than once" by difference threads also. E.g. main() joins with thread1 and thread2 joins with thread1. This is invalid too.
It's also thread1. Remember it's not necessary to call pthread_join() on every thread. It's just an example.
|
1

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

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

2 Comments

Say that, Main thread creates thread1 and thread2. 1 - Does it possible "main thread and thread2 simultaneously join thread2"? 2 - Does it possible "thread1 join thread2", in other words can a thread be joined by a thread which is not its parent?
@oiyio: The answer to both is in the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.