I an learning pthread
and I have a few questions.
Here is my code:
#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #define NUM_THREADS 10 using namespace std; void *PrintHello(void *threadid) { int* tid; tid = (int*)threadid; for(int i = 0; i < 5; i++){ printf("Hello, World (thread %d)\n", *tid); } pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc; int t; int* valPt[NUM_THREADS]; for(t=0; t < NUM_THREADS; t++){ printf("In main: creating thread %d\n", t); valPt[t] = new int(); *valPt[t] = t; rc = pthread_create(&threads[t], NULL, PrintHello, (void *)valPt[t]); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } /* Last thing that main() should do */ pthread_exit(NULL); }
The code runs well and I don't call pthread_join
. So I want to know, is pthread_join
a must?
Another issue, is:
valPt[t] = new int(); *valPt[t] = t; rc = pthread_create(&threads[t], NULL, PrintHello, (void *)valPt[t]);
equal to:
rc = pthread_create(&threads[t], NULL, PrintHello, &i);