OHHH
OHHH

Reputation: 1051

In C, why should you call pthread_join only after initializing a list of created threads?

I understand what both do, but I have a question about the behavior of pthread_join.

I noticed this pattern is pretty common in C (mind the pseudocode):

for i in some range:
    thread[i] = pthread_create (...)

for i in some range:
    pthread_join(&thread[i]...)

Why can't it happen at the same time? e.g:

for i in some range:
    thread[i] = pthread_create (...)
    pthread_join(&thread[i]...)

Upvotes: 0

Views: 101

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140186

because if you do that, all threads run sequentially, since pthread_join waits for thread termination.

So you're losing the benefit of running the threads at the same time, simply.

Upvotes: 2

Related Questions