multithreading process in C++, all threads are ended up without completion -


i run following code using pthread.h... while run, before thread finishes, code exits...

i attached code...

#include<iostream> #include<pthread.h>  using namespace std;  #define num_threads 5  void *printhello(void *threadid) {     long tid = (long)threadid;     cout<<"hello world! thread id,"<<tid<<endl;     pthread_exit(null);     return &tid; }  int main() {     pthread_t threads[num_threads];     int rc;     int i;      for(i=0;i<num_threads;i++)     {         cout<<"main() : creating thread,"<<i<<endl;         rc = pthread_create(&threads[i],null,printhello,(void*)i);         //sleep(1);         if(rc)         {             cout<<"error:unable create thread,"<<rc<<endl;             exit(-1);         }     }      pthread_exit(null);      return 0; } 

you should join threads before call pthread_exit in main.

for (i = 0; < num_threads; i++) {    pthread_join(threads[i], 0); } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -