Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make main program wait for threads to finish

In the following code I create some number of threads, and each threads sleeps for some seconds.

However my main program doesn't wait for the threads to finish, I was under the assumption that threads would continue to run until they finished by themselves.

Is there someway of making threads continue to run even though the calling thread finishes.

#include <pthread.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>


int sample(int min,int max){
  int r=rand();
  return (r %max+min );
}



void *worker(void *p){
  long i = (long) p;
  int s = sample(1,10);
  fprintf(stdout,"\tid:%ld  will sleep: %d \n",i,s);
  sleep(s);
  fprintf(stdout,"\tid:%ld  done sleeping \n",i,s);
}

pthread_t thread1;

int main(){
  int nThreads = sample(1,10);

  for(int i=0;i<nThreads;i++){
    fprintf(stderr,"\t-> Creating: %d of %d\n",i,nThreads);
    int iret1 = pthread_create( &thread1, NULL, worker, (void*) i);
    pthread_detach(thread1);
  }
  //  sleep(10);//work if this is not commented out.
  return 0;
}

Thanks

Edit:

Sorry for not clarifying, is it possible without explicitly keeping track of my current running threads and by using join.

like image 807
monkeyking Avatar asked Jun 24 '11 12:06

monkeyking


People also ask

How do I make the main thread wait in Python?

We can wait for a result using sleep. Specifically, the waiting thread can call the time. sleep() function and specify a number of seconds to wait. The thread will then block until the number of seconds has elapsed, before checking whether the new thread has completed and returned a result.

What is the code if main thread should wait until all the other threads are finished?

pthread_join() — Wait for a thread to end.

Which method is used to make the main thread wait for all child threads to finish?

1. Which of this method can be used to make the main thread to be executed last among all the threads? Explanation: By calling sleep() within main(), with long enough delay to ensure that all child threads terminate prior to the main thread.


3 Answers

Each program has a main thread. It is the thread in which your main() function executes. When the execution of that thread finishes, the program finishes along with all its threads. If you want your main thread to wait for other threads, use must use pthread_join function

like image 188
Armen Tsirunyan Avatar answered Oct 21 '22 14:10

Armen Tsirunyan


You need to keep track of the threads. You are not doing that because you are using the same thread1 variable to every thread you are creating.

You track threads by creating a list (or array) of pthread_t types that you pass to the pthread_create() function. Then you pthread_join() those threads in the list.

edit:

Well, it's really lazy of you to not keep track of running threads. But, you can accomplish what you want by having a global var (protected by a mutex) that gets incremented just before a thread finishes. Then in you main thread you can check if that var gets to the value you want. Say nThreads in your sample code.

like image 27
Vinicius Kamakura Avatar answered Oct 21 '22 15:10

Vinicius Kamakura


You need to join each thread you create:

int main()
{
    int                    nThreads = sample(1,10);
    std::vector<pthread_t> threads(nThreads);

    for(i=0; i<nThreads; i++)
    {
            pthread_create( &threads[i], NULL, worker, (void*) i)
    }

    /* Wait on the other threads */
    for(i=0; i<nThreads; i++)
    {
            status*   status;
            pthread_join(threads[i], &status);
    }
}
like image 44
Martin York Avatar answered Oct 21 '22 15:10

Martin York