How to reuse a pthreads?

2

I'm using pthread.h and using the following functions:

//
// Cria A thread
//
thread_argsPP[contthreadsPP]        =   contthreadsPP;
pthread_create(&threadsPP[contthreadsPP], NULL,  ReceivePinPad, (void *)    &thread_argsPP[contthreadsPP]);

//
//  Inicia a thread
//
pthread_join(threadsPP[contthreadsPP], NULL);

//
//  Fecha a thread Recive
//
pthread_detach(pthread_self());
pthread_exit(NULL);

But after closing the thread I can not recreate it, I would like to know if it has any way to reuse a pthread after pthread_exit(NULL) .

    
asked by anonymous 21.07.2016 / 15:13

1 answer

0

The function pthread_exit , waits for all Threads to be terminated to terminate the system. Then it would be the same as using% w /%.

But if you want to reuse the threads, you can use a counter to wait for them to finish, you can use a counter to know how many threads are running.

int count;

void thread_call(void *data){
    ...
    count--;
}

void main(){
    ...
    for(...){ // cria as threads
        ...
        count++;
    }
    while(count); // espera as threads terminarem;
    for(...){ // re-instancia as threads
        ...
        count++;
    }

    pthread_exit(0);
}
    
23.07.2016 / 07:54