zoukankan      html  css  js  c++  java
  • advacing lnux program 4.1.5 Thread Attributes[copy]

    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 thread
    attribute object. If you pass a null pointer, the default thread attributes are used to
    configure the new thread. However, you may create and customize a thread attribute
    object 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 necessary
    to keep the thread attribute object around after the threads have been created.


    For most GNU/Linux application programming tasks, only one thread attribute is
    typically of interest (the other available attributes are primarily for specialty real-time
    programming).This attribute is the thread’s detach state. A thread may be created as a
    joinable thread (the default) or as a detached thread. A joinable thread, like a process, is not
    automatically cleaned up by GNU/Linux when it terminates. Instead, the thread’s exit
    state hangs around in the system (kind of like a zombie process) until another thread
    calls pthread_jointo obtain its return value. Only then are its resources released. A
    detached thread, in contrast, is cleaned up automatically when it terminates. Because a
    detached thread is immediately cleaned up, another thread may not synchronize on its
    completion 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 the
    desired detach state. Because the joinable state is the default, it is necessary to call this only
    to 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 thread
    attribute 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 detached
    thread.To do this, call pthread_detach. Once a thread is detached, it cannot be made
    joinable again.

    // 等待线程如何clean  up 呢 ????

  • 相关阅读:
    个人应该做什么样的网站?
    得对xls文件作操作
    正则替换
    31 反馈的判断方法(2)
    AD 层次原理图设计 自上而下
    27 多级放大电路的频率响应与第四章集成运放的电流源电路
    P28 集成运放的电流源电路(2)
    32 负反馈放大电路的方块图
    ESP8266固件AT指令连接EMQ 的MQTT服务器
    33 深度负反馈的实质与放大倍数分析
  • 原文地址:https://www.cnblogs.com/michile/p/2890784.html
Copyright © 2011-2022 走看看