What is the use of PTHREAD_CREATE_JOINABLE in pthread?

I read some codes as below:

void mcachefs_file_start_thread() { pthread_attr_t attrs; pthread_attr_init(&attrs); pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_JOINABLE); pthread_create(&mcachefs_file_threadid, &attrs, mcachefs_file_thread, NULL); } 

In this context, setting attrs as PTHREAD_CREATE_JOINABLE ? Also, isn't this the default attribute of the stream created by pthead_create ?

+7
source share
2 answers

Yes, PTHREAD_CREATE_JOINABLE is the default attribute. The goal is that it allows you to call pthread_join on a thread, which is a function that waits for a thread to finish and will give you a return value if its main procedure.

Sometimes, when you create a thread to do some background work, it might be a good idea to make sure it is finished before using its results or moving on to something else. For which combined threads for.

+11
source

In the posix parameter, the default value of the disconnect state attribute in the new initialized stream attribute object is indeed PTHREAD_CREATE_JOINABLE. See For example http://linux.die.net/man/3/pthread_attr_setdetachstate So you are right: the line of code pthread_attr_setdetachstate is not needed in the code snippet.

+3
source

All Articles