Linux 线程属性相关函数

pthread_attr_t就是对应线程的属性

bash 复制代码
/*
    #include <pthread.h>
    int pthread_attr_init(pthread_attr_t *attr);
        初始化线程属性变量
    int pthread_attr_destroy(pthread_attr_t *attr);
        释放线程属性资源
    int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
        获取线程分离的状态属性
    int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
        设置线程分离的状态属性
*/

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>

void* callback(void* arg) {
    printf("child thread id:%ld", pthread_self());
    return NULL;
}

int main() {

    //创建一个线程属性变量
    pthread_attr_t attr;
    //初始化
    pthread_attr_init(&attr);
    //设置属性
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    //创建一个子线程
    pthread_t tid;
    int ret = pthread_create(&tid, &attr, callback, NULL);
    if(ret != 0) {
        char* strerr = strerror(ret);
        printf("error: %s\n", strerr);
    }

    printf("tid: %ld, main thread id:%ld\n", tid, pthread_self());

    size_t size;
    pthread_attr_getstacksize(&attr, &size);

    printf("%ld\n", size);
    pthread_attr_destroy(&attr);

    pthread_exit(NULL);

    return 0;
}
相关推荐
Everbrilliant8918 分钟前
Ubuntu系统下交叉编译Android的X265库
linux·运维·ubuntu·x265交叉编译·android x265·ffmpeg x265
我不要放纵26 分钟前
LVS集群搭建
linux·服务器·lvs
阿巴~阿巴~30 分钟前
自主Shell命令行解释器
linux·运维·服务器
许白掰31 分钟前
Linux入门篇学习——借助 U 盘或 TF 卡拷贝程序到开发板上
linux·学习·借助 u 盘拷贝程序到开发板上·借助 tf卡拷贝程序到开发板上
小周学学学32 分钟前
docker安装与简单项目上手
运维·docker·容器
SHUIPING_YANG1 小时前
根据用户id自动切换表查询
java·服务器·数据库
chao_7891 小时前
更灵活方便的初始化、清除方法——fixture【pytest】
服务器·自动化测试·python·pytest
枷锁—sha2 小时前
【DVWA系列】——CSRF——Medium详细教程
android·服务器·前端·web安全·网络安全·csrf
枷锁—sha2 小时前
跨站请求伪造漏洞(CSRF)详解
运维·服务器·前端·web安全·网络安全·csrf
scuter_yu2 小时前
腾讯云云服务器深度介绍
服务器·云计算·腾讯云