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);
}
Here, what is the usage of setting attrs as PTHREAD_CREATE_JOINABLE? Besides, isn’t it the default attribute of a thread created by pthead_create?
Yes,
PTHREAD_CREATE_JOINABLEis the default attribute. The purpose is that it allows you to callpthread_joinon the thread, which is a function that waits until the thread finishes, and gives you return value if its main routine.Sometimes, when you’re creating a thread to do some background work, it might be a good idea to make sure it has finished before you use its results or move to something else. That’s what joinable threads are for.