Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1521581
  • 博文数量: 290
  • 博客积分: 3468
  • 博客等级: 中校
  • 技术积分: 3461
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-28 22:21
文章分类

全部博文(290)

文章存档

2016年(13)

2015年(3)

2014年(42)

2013年(67)

2012年(90)

2011年(75)

分类: C/C++

2014-07-10 23:01:49

在《linux 编程技术详解》这本书中讲到线程属性pthread_attr_t的定义是:

typedef struct

{

       int                               __detachstate;   线程的分离状态

       int                               __schedpolicy;  线程调度策略

       struct sched_param              __schedparam;  线程的调度参数

       int                               __inheritsched;  线程的继承性

       int                                __scope;       线程的作用域

       size_t                           __guardsize;   

       int                                __stackaddr_set;

       void *                          __stackaddr;   线程堆栈的位置

       size_t                           __stacksize;    线程栈的大小

}pthread_attr_t;


我在/usr/include/bits/pthreadtypes.h这个文件中找到pthread_attr_t的定义却是这样的:


typedef union
{
  char __size[__SIZEOF_PTHREAD_ATTR_T];
  long int __align;
} pthread_attr_t;

     之所以出现和两种不同的定义可能是因为pthread不想让用户空间看到它内部对pthread_attr_t的实现,因为API设计时就封装了这个类型。所以它只要提供和内部实现的pthread_attr_t大小一样的类型就可以了。

实际上,pthread内部对pthread_attr_
t的实现是
struct pthread_attr,它的内容和书上的内容基本上是一致的。同时在机子上测试可以看到这两个结构体的大小是相等的。


阅读(930) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~