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;
}
相关推荐
jerryinwuhan4 小时前
《系统部署与运维》开篇 课程介绍
运维
ltl6 小时前
实时 OS 巡礼:VxWorks、QNX、Zephyr 与 PREEMPT_RT
linux
我是谁??6 小时前
Ubuntu22.04更换清华源
linux·运维·服务器
圣殿骑士-Khtangc6 小时前
Go字符串高效拼接性能对比与底层原理分析
服务器·前端·golang
Shell运维手记7 小时前
Linux 常用基础命令学习笔记
linux·运维·笔记·学习·算法·github
朴马丁7 小时前
国际与国产PLM在精细化工赛道的布局:2026年主要厂商技术特色
大数据·运维·人工智能·流程行业plm·化工新材料
这个DBA有点耶7 小时前
数据库一体机架构演进:从硬件堆叠到软硬深度耦合
服务器·网络·数据库·硬件架构·运维开发·database·数据库架构
Elastic 中国社区官方博客8 小时前
Elasticsearch:使用 AI Agent 来创建 workflow
大数据·运维·人工智能·elasticsearch·搜索引擎·自动化·全文检索
测试运维日常笔记8 小时前
Linux系统安装配置TigerVNC服务器完整指南
linux·运维·服务器
像风一样自由20208 小时前
加强版VScode结合remote-ssh远程连接服务器开发指南
服务器·vscode·ssh