pthread_attr_t 线程属性(二)
一.函数:
1.线程属性的初始化与销毁:
#include <pthread.h>
int pthread_attr_init(pthread_attr_t *attr);
int pthread_attr_destroy(pthread_attr_t *attr);
Both return: 0 if OK, error number on failure
2.设置线程属性--detackstate(分离状态):
#include <pthread.h>
int pthread_attr_getdetachstate(const pthread_attr_t *restrict attr, int *detachstate);
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
Both return: 0 if OK, error number on failure
detachstate有两个选项:PTHREAD_CREATE_DETACHED 分离状态启动线程
PTHREAD_CREATE_JOINABLE 正常状态启动线程
3.设置线程属性--stackaddr(线程栈的最低地址),stacksize(线程栈的大小):
#include <pthread.h>
int pthread_attr_getstack(const pthread_attr_t *restrict attr,
void **restrict stackaddr,
size_t *restrict stacksize);
int pthread_attr_setstack(const pthread_attr_t *attr,
void *stackaddr,
size_t *stacksize);
Both return: 0 if OK, error number on failure
4.设置线程属性--stacksize(线程栈的大小):
#include <pthread.h>
int pthread_attr_getstacksize(const pthread_attr_t *restrict attr,
size_t *restrict stacksize);
int pthread_attr_setstacksize(pthread_attr_t *attr,
size_t stacksize);
Both return: 0 if OK, error number on failure
5.设置线程属性--guardsize(线程栈末尾的警戒缓冲区大小)
#include <pthread.h>
int pthread_attr_getguardsize(const pthread_attr_t *restrict attr,
size_t *restrict guardsize);
int pthread_attr_setguardsize(pthread_attr_t *attr,
size_t guardsize);
Both return: 0 if OK, error number on failure
二.重点:
三.例子:
以分离状态创建线程
- #include <stdio.h>
- #include <pthread.h>
- #include <string.h>
- void * thr_fn()
- {
- printf("thread run\n");
- pthread_exit((void *)0);
- }
- int main()
- {
- pthread_t tid;
- pthread_attr_t attr;
- int ret;
- ret = pthread_attr_init(&attr);
- if(ret!=0)
- printf("init attr error:%s\n",strerror(ret));
- ret = pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
- if(ret==0)
- {
- ret = pthread_create(&tid,&attr,thr_fn,NULL);
- if(ret!=0)
- printf("create thread error:%s\n",strerror(ret));
- }
- pthread_attr_destroy(&attr);
- sleep(1);
- return 0;
- }
运行:
root@ubuntu1:~/12# ./a.out
thread run
最新文章
- PHP编码规范PSR-2
- 请将项目文件中的“AutoGenerateBindingRedirects”属性设置为 true 警告!!!
- nginx-502错误,老是提示busy.优化php-fpm如下
- linux笔记二-----目录及文件命令
- z/OS上Dataset 的移动
- 关于win7和xp的屏设置类
- UIkit框架之UIimage
- Java中的字符串流的读取和写入(创建文件并判断重复账户)
- Oracle除去换行符的方法
- iOS UISearchDisplayController学习笔记
- JavaScript基础——变量、语句、注释
- 学习MVC之租房网站(十)-预约和跟单
- 『HTMl5』学习日志
- struts2的简单执行过程
- cs231n spring 2017 lecture16 Adversarial Examples and Adversarial Training 听课笔记
- Python Cookbook(第3版)中文版:15.21 诊断分段错误
- [LVM]创建LVM卷
- SQL分组函数
- JAVA进阶1
- 理解ES6中的Promise
热门文章
- 李洪强iOS开发之OC点语法和变量作用域
- lintcode:删除链表中指定元素
- ffmpeg转码时对编码率和固定码率的处理
- AdventureWorksDW2008R2 attach: Unable to open the physical file. Operating system error 5: ";5(Access is denied.)
- 卷积神经网络和CIFAR-10:Yann LeCun专访 Convolutional Nets and CIFAR-10: An Interview with Yann LeCun
- Data Base MongoDB 插入时间不正确的问题
- flex 生命周期 ibm引用
- Java用于取得当前日期相对应的月初,月末,季初,季末,年初,年末时间
- sgen.exe"; exited with code 1.解决方法
- Hook入门