Thread attributes provide a mechanism for fine-tuning the behavior of individual
threads. Recall that pthread_createaccepts an argument that is a pointer to a threadattribute object. If you pass a null pointer, the default thread attributes are used toconfigure the new thread. However, you may create and customize a thread attributeobject to specify other values for the attributes.To specify customized thread attributes, you must follow these steps:1. Create a pthread_attr_tobject.The easiest way is simply to declare an auto-matic variable of this type. |声明pthread_attr_tobject变量2. Call pthread_attr_init, passing a pointer to this object.This initializes the |调用pthread_attr_init函数 attributes to their default values.3. Modify the attribute object to contain the desired attribute values. |根据需要更改属性对象4. Pass a pointer to the attribute object when calling pthread_create. |pthread_create中传递属性对象5. Call pthread_attr_destroyto release the attribute object.The pthread_attr_t |调用pthread_attr_destroy销毁对象variable itself is not deallocated; it may be reinitialized with pthread_attr_init.A single thread attribute object may be used to start several threads. It is not necessaryto keep the thread attribute object around after the threads have been created. For most GNU/Linux application programming tasks, only one thread attribute istypically of interest (the other available attributes are primarily for specialty real-timeprogramming).This attribute is the thread’s detach state. A thread may be created as ajoinable thread (the default) or as a detached thread. A joinable thread, like a process, is notautomatically cleaned up by GNU/Linux when it terminates. Instead, the thread’s exitstate hangs around in the system (kind of like a zombie process) until another threadcalls pthread_jointo obtain its return value. Only then are its resources released. Adetached thread, in contrast, is cleaned up automatically when it terminates. Because adetached thread is immediately cleaned up, another thread may not synchronize on itscompletion by using pthread_joinor obtain its return value.To set the detach state in a thread attribute object, use pthread_attr_setdetachstate.The first argument is a pointer to the thread attribute object, and the second is thedesired detach state. Because the joinable state is the default, it is necessary to call this onlyto create detached threads; pass PTHREAD_CREATE_DETACHEDas the second argument.The code in Listing 4.5 creates a detached thread by setting the detach state threadattribute for the thread.Listing 4.5 (detached.c) Skeleton Program That Creates a Detached Thread#include <pthread.h>void* thread_function (void* thread_arg){ /* Do work here... */}int main (){ pthread_attr_t attr;pthread_t thread;pthread_attr_init (&attr);pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);pthread_create (&thread, &attr, &thread_function, NULL);pthread_attr_destroy (&attr);/* Do work here... *//* No need to join the second thread. */return 0;} Even if a thread is created in a joinable state, it may later be turned into a detachedthread.To do this, call pthread_detach. Once a thread is detached, it cannot be madejoinable again.// 等待线程如何clean up 呢 ????