Вы можете сделать это с помощью следующей последовательности:
pthread_create thread1
pthread_create thread2
pthread_join thread1
pthread_join thread2
Другими словами, запустите все свои темы, прежде чем пытаться присоединиться к любым из них.Более подробно, вы можете начать с чего-то вроде следующей программы:
#include <stdio.h>
#include <pthread.h>
void *myFunc (void *id) {
printf ("thread %p\n", id);
return id;
}
int main (void) {
pthread_t tid[3];
int tididx;
void *retval;
// Try for all threads, accept less.
for (tididx = 0; tididx < sizeof(tid) / sizeof(*tid); tididx++)
if (pthread_create (&tid[tididx], NULL, &myFunc, &tid[tididx]) != 0)
break;
// Not starting any is pretty serious.
if (tididx == 0)
return -1;
// Join to all threads that were created.
while (tididx > 0) {
pthread_join (tid[--tididx], &retval);
printf ("main %p\n", retval);
}
return 0;
}
Это попытается запустить три потока перед тем, как присоединиться к любому, а затем присоединится ко всем тем, что ему удалось запустить,в обратном порядке.Выход, как и ожидалось, составляет:
thread 0x28cce4
thread 0x28cce8
thread 0x28ccec
main 0x28ccec
main 0x28cce8
main 0x28cce4