一.函数:

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
二.重点:

三.例子:

以分离状态创建线程

  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <string.h>
  4. void * thr_fn()
  5. {
  6. printf("thread run\n");
  7. pthread_exit((void *)0);
  8. }
  9. int main()
  10. {
  11. pthread_t tid;
  12. pthread_attr_t attr;
  13. int ret;
  14. ret = pthread_attr_init(&attr);
  15. if(ret!=0)
  16. printf("init attr error:%s\n",strerror(ret));
  17. ret = pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
  18. if(ret==0)
  19. {
  20. ret = pthread_create(&tid,&attr,thr_fn,NULL);
  21. if(ret!=0)
  22. printf("create thread error:%s\n",strerror(ret));
  23. }
  24. pthread_attr_destroy(&attr);
  25. sleep(1);
  26. return 0;
  27. }

运行:
root@ubuntu1:~/12# ./a.out
thread run

最新文章

  1. PHP编码规范PSR-2
  2. 请将项目文件中的“AutoGenerateBindingRedirects”属性设置为 true 警告!!!
  3. nginx-502错误,老是提示busy.优化php-fpm如下
  4. linux笔记二-----目录及文件命令
  5. z/OS上Dataset 的移动
  6. 关于win7和xp的屏设置类
  7. UIkit框架之UIimage
  8. Java中的字符串流的读取和写入(创建文件并判断重复账户)
  9. Oracle除去换行符的方法
  10. iOS UISearchDisplayController学习笔记
  11. JavaScript基础——变量、语句、注释
  12. 学习MVC之租房网站(十)-预约和跟单
  13. 『HTMl5』学习日志
  14. struts2的简单执行过程
  15. cs231n spring 2017 lecture16 Adversarial Examples and Adversarial Training 听课笔记
  16. Python Cookbook(第3版)中文版:15.21 诊断分段错误
  17. [LVM]创建LVM卷
  18. SQL分组函数
  19. JAVA进阶1
  20. 理解ES6中的Promise

热门文章

  1. 李洪强iOS开发之OC点语法和变量作用域
  2. lintcode:删除链表中指定元素
  3. ffmpeg转码时对编码率和固定码率的处理
  4. AdventureWorksDW2008R2 attach: Unable to open the physical file. Operating system error 5: &quot;5(Access is denied.)
  5. 卷积神经网络和CIFAR-10:Yann LeCun专访 Convolutional Nets and CIFAR-10: An Interview with Yann LeCun
  6. Data Base MongoDB 插入时间不正确的问题
  7. flex 生命周期 ibm引用
  8. Java用于取得当前日期相对应的月初,月末,季初,季末,年初,年末时间
  9. sgen.exe&quot; exited with code 1.解决方法
  10. Hook入门