Reputation: 31
I’m using pthread to start a thread, but the create function itself is not starting the thread But instead if i call pthread join, it starts so actually should i call p thread join?
i heard from some answers that pthread join is not required , my pthread create function is called from main
int main(void) {
printf("-----------Welcome------\n");
Start_Generation();
return 0;
}
int Start_Generation(void)
{
ret=pthread_create(&C_AO_GENERATION_THREAD, NULL, C_Aogeneration_thread,(void*) message1);
if(ret!=0)
{
printf("Error Starting Thread\n");
return -1;
}
else
{
printf("AO Thread Started\n");
}
//ret=pthread_join(C_AO_GENERATION_THREAD, NULL);
return 0;
}
void *C_Aogeneration_thread (void *ptr)
{
char *message;
message = (char *) ptr;
unsigned int prof_gen_count;
unsigned int written_sample=0;
unsigned int curr_idx=0;
int ch=0;
int written=0;
printf("%s",message);
printf(" Generation Started\n");
prof_gen_count=(samplingrate/10);
while(stopflag)
{
printf("Thread is working\n");
}
return 0;
}
enter code here
Upvotes: 0
Views: 1378
Reputation: 181339
I’m using pthread to start a thread, but the create function itself is not starting the thread
As presented in the question, your main thread terminates by returning from main
immediately after reporting on the success or failure of the pthread_create()
call, so
the whole program terminates at that point, apparently before the child progresses far enough to present any evidence that it is running, but also
there is no point in using a separate thread.
But instead if i call pthread join, it starts so actually should i call p thread join?
A call to pthread_join()
does not return successfully until the specified thread has terminated. That is, in fact, its purpose. As a rule of thumb, you should either join or detach every thread you create. Joining is usually what you want, and note, too, that detaching a thread does not prevent the process from terminating and taking that thread with it when the main thread returns or exit()
s.
Upvotes: 3