pthread_t definition

近期在看google的chromium的代码,认为其基础库base中的对于与平台有关的线程的数据结构的定义与其代码中的凝视部分不匹配。

 

// PlatformThreadHandle should not be assumed to be a numeric type, since the // standard intends to allow pthread_t to be a structure. This means you // should not initialize it to a value, like 0. If it's a member variable, the // constructor can safely "value initialize" using () in the initializer list. #if defined(OS_WIN) #include <windows.h> typedef DWORD PlatformThreadId; typedef void* PlatformThreadHandle; // HANDLE const PlatformThreadHandle kNullThreadHandle = NULL; #elif defined(OS_POSIX) #include <pthread.h> typedef pthread_t PlatformThreadHandle; const PlatformThreadHandle kNullThreadHandle = 0; #if defined(OS_MACOSX) #include <mach/mach.h> typedef mach_port_t PlatformThreadId; #else // OS_POSIX && !OS_MACOSX #include <unistd.h> typedef pid_t PlatformThreadId; #endif #endif 

 

凝视明白说明 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的确被定义为一个结构体:

 

/* * Generic handle type - intended to extend uniqueness beyond * that available with a simple pointer. It should scale for either * IA-32 or IA-64. */ typedef struct { void * p; /* Pointer to actual object */ unsigned int x; /* Extra information - reuse count etc */ } ptw32_handle_t; typedef ptw32_handle_t pthread_t; 

 

从而导致例如以下代码不具有可移植性:

 

pthread_t tid;

.....

tid = (pthread_t)0;

 

正确的实现应该是用memset()来将其初始化为0,虽然在有的系统的实现中,将pthread_t的变量初始化为0也未必是一个正确的选择。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值