Linux线程的属性--相关函数

参考:https://blog.csdn.net/pbymw8iwm/article/details/6721038

Linux多线程实践(三)线程的基本属性设置APIhttps://www.cnblogs.com/zsychanpin/p/6940673.html


线程属性结构如下:

typedef struct

{

       int                               detachstate;   线程的分离状态

       int                               schedpolicy;  线程调度策略

       structsched_param              schedparam;  线程的调度参数

       int                               inheritsched;  线程的继承性

       int                                scope;       线程的作用域

       size_t                           guardsize;   线程栈末尾的警戒缓冲区大小

       int                                stackaddr_set;

       void*                          stackaddr;   线程栈的位置

       size_t                           stacksize;    线程栈的大小

}pthread_attr_t;


设置线程属性,一般在创建线程时来指定,pthread_create的第二个参数就是线程属性,该形参传入NULL意味着使用默认属性。如果要自定义线程的属性,应当填充一个pthread_attr_t结构变量并作为实参传进去。

相关函数:

pthread_attr_init

pthread_attr_setXXXX

pthread_attr_getXXXX

pthread_attr_destroy


 pthread_t  tid;//子线程ID
        pthread_attr_t attr;//线程属性

        pthread_attr_init(&attr);//把attr结构的成员都初始化为默认值(合法值)
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//修改属性成员:分离态
        pthread_attr_setstacksize(&attr, 2048);//修改属性成员: 堆栈大小
        //属性结构体的每一项属性成员都有对应的set和get函数,请查看上面的参考链接

        //创建子线程
        status = pthread_create(&tid, &attr, thread_entry, NULL);
        if(0 == status)
        {
                printf("creat thread ok, tid = %ld, with para: %d\n", tid, arg);
        }
        else
        {
                printf("creat thread failed, error info: %s\n", strerror(errno));
                return -1;
        }

        pthread_attr_destroy(&attr);//属性值使用完必须销毁
              

猜你喜欢

转载自blog.csdn.net/qq_31073871/article/details/80782385