I am trying to execute a function in a separate thread and I have noticed that sometimes the thread is executing twice.
If i also call pthread_join, the the code is executing only once every time I run the program.
I do not understand why this is happening.
I am attaching the code:
#include <stdio.h>
#include <pthread.h>
int func(){
printf("Printing from a thread\n");
return 0;
}
int main(void) {
pthread_t ptid;
pthread_create(&ptid,NULL,&func,NULL);
//pthread_join(ptid,NULL);
printf("Hello World\n");
return 0;
}
EDIT: I have modified the code so the function has the correct header. I have attached the new code. The issue is still present.
#include <stdio.h>
#include <pthread.h>
void* func(void* arg){
printf("Printing from a thread\n");
return 0;
}
int main(void) {
pthread_t ptid;
pthread_create(&ptid,NULL,&func,NULL);
//pthread_join(ptid,NULL);
printf("Hello World\n");
return 0;
}