线程问题。急急急
linux吧
全部回复
仅看楼主
level 1
在《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;
1、为什么会出现这两种不同的情况?
2、如何查看pthread_attr_t结构的第一种定义
2017年12月30日 16点12分 1
level 13
0、pthread上层用户,访问attr应使用pthread_attr_xxx这些接口,不需要(也最好不要)直接访问pthread_attr_t的成员;但pthread实现本身是有直接访问pthread_attr_t成员需求的,故pthread_attr_t需要具体的定义
1、上层使用init接口时,attr结构空间分配在pthread上层,故需要一个定义,能确保有充足空间供pthread实现使用,后一种结构定义的作用就在于 “确保空间充足”
当然,在某个确定平台直接提供类似前一种之类的定义也是可以的,但attr逻辑上是平台相关的玩意,不提供具体定义给用户去访问,也是为了能令用户的代码具备基本的可移植性
2、不需要知道他的定义,一定想知道,可以看看pthread的实现,例如glibc实现
2017年12月30日 17点12分 4
level 7
链接在bits里,是不是把上面一大坨都编码进下面了
2017年12月31日 23点12分 5
从定义上看,应该是这样的,可是追踪 不到第一种定义啊
2018年01月01日 04点01分
level 13
看来你没懂4L的解释。直接帮你找到好了。这边只列举两种pthread实现,有其他实现列举需求的请回复告知:
1、glibc
gnu/linux下,使用的pthread实现,通常由glibc提供,在glibc-2.26源码的sysdeps/nptl/internaltypes.h文件中可以找到struct pthread_attr结构,和接口层使用的pthread_attr_t实际上是一个类型
2、freebsd
freebsd有自己的pthread实现,在freebsd/lib/libthr/thread/thr_private.h中定义的struct pthread_attr也就是接口用的pthread_attr_t
ps:这两种实现中的struct pthread_attr定义就不一样。由此顺带重申下,请勿直接使用pthread_attr_t的成员,即使你知道他们。原因很简单,便于代码移植。
2018年01月01日 05点01分 6
明白了,谢谢
2018年01月05日 14点01分
1