Hi guys, I have a question about some code that I am testing to begin to understand posix streams.
I have this base code:
#include <iostream> #include <string> #include <sstream> #include <pthread.h> using namespace std; void *printInfo(void *thid){ long tid; tid =(long)thid; printf("Hello from thread %ld.\n",tid); pthread_exit(NULL); } int main (int argc, char const *argv[]) { int num =8; pthread_t threadlist[num]; int rc; long t; for(t=0;t<num;t++){ printf("Starting thread %ld\n",t); rc = pthread_create(&threadlist[t],NULL,printInfo,(void *)t); if(rc) { printf("Error creating thread"); exit(-1); } } pthread_exit(NULL); return 0; }
Very simple code, starting threads and printing from them, everything works wonders, except that I do not understand the last pthread_exit (NULL) before returning 0; at the end of the main method.
It seems that the main thread should not be pthread, and it does not need this! If I donβt say it, the code does not work (this means that the code is compiled and executed, but I only receive messages "start thread" hte "start thread", and not messages "hello from ..".
So basically I want to know why this is needed.
In addition, the code compiles and runs correctly if I comment on include
source share