最近在看google的chromium的代码,觉得其基础库base中的对于与平台有关的线程的数据结构的定义与其代码中的注释部分不匹配。
注释明确说明 phtread_t在标准中有被定义为一个结构体的可能性,但其在实际代码中仍然出现了:
#include <pthread.h>
typedef pthread_t PlatformThreadHandle;
const PlatformThreadHandle kNullThreadHandle = 0;
显然此写法肯定不是个错误,否则chromium在posix系统上就编译不过去了。
为此专门search了一下pthread_t的定义,发现很真是有多种定义方法,
在linux的实现中pthread_t被定义为 "unsigned long int", 参见如下详细说明:
With LinuxThreads (the default Pthreads library on 2.4 kernel), the
pthread_t was in fact related to an index of an internal table. With
NPTL, the pthread_t holds the memory address of a structure that
describes the thread properties.
在Windows中pthread_t的确被定义为一个结构体:
从而导致如下代码不具有可移植性:
pthread_t tid;
.....
tid = (pthread_t)0;
正确的实现应该是用memset()来将其初始化为0,尽管在有的系统的实现中,将pthread_t的变量初始化为0也未必是一个正确的选择。
本文探讨了Chromium项目中base库的线程管理模块,特别是针对不同平台(如POSIX和Windows)下线程数据结构的定义差异进行了讨论。文章分析了pthread_t类型在不同系统中的具体实现,并指出了在代码中初始化该类型变量时可能遇到的可移植性问题。
1015

被折叠的 条评论
为什么被折叠?



