近期在看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也未必是一个正确的选择。

pthread_t definition的更多相关文章

  1. foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.Table' because 'System.Web.UI.WebControls.Table' does not contain a public definition for 'GetEnumerator'

    错误:foreach statement cannot operate on variables of type 'System.Web.UI.WebControls.Table' because ' ...

  2. `UnityEditor.EditorUtility' does not contain a definition for `GetMiniThumbnail'

    I got the following errors with Untiy 4.0f7error CS0117: `UnityEditor.EditorUtility' does not contai ...

  3. 原创: How to build a query based on Definition Updates installed

    In SCCM 2012 R2, you can use following class. Use SMS_CombinedDeviceResources.EPAntivirusSignatureLa ...

  4. Definition of success-成功的定义

    "My definition of success is doing what you love. I fell many people do things because they fee ...

  5. XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式

    XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD),作用是定义 XML 文档的合法构建模块,类似 DTD,但更加强大. 作用有: ①定义 ...

  6. TFS Build Definition And Auto Deploy

    一台build machine上一般只有一个build service[对应一个build controller]来serve一个team project collection,但又workaroun ...

  7. multiple definition of `err_sys' 《UNIX环境高级编程》

    本文地址:http://www.cnblogs.com/yhLinux/p/4079930.html 问题描述: [点击此处直接看解决方案] 在练习<UNIX环境高级编程>APUE程序清单 ...

  8. Mysql运行SQL文件 错误Incorrect table definition;there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

    问题描述 想从服务器上DOWN下数据库.操作:先把数据库转存为SQL文件,然后在本地利用navicate运行SQL文件,出现错误信息: Incorrect table definition;there ...

  9. Tomcat7.0启动报错:java.lang.illegalargumentexception:taglib definition not consisten with specification version

    Tomcat7.0启动报错:java.lang.illegalargumentexception:taglib definition not consisten with specification ...

随机推荐

  1. go - 复合类型 array, slice, map

    Go 语言支持复合类型: 数组:array 切片:slice 指针:pointer 字典:map 通道:chan 结构体:struct 接口:interface 1. array   同一类型数据的集 ...

  2. c++编程碰到的奇怪问题与解决

    今天写一个工具,调试过程中莫名其妙崩溃,类某些成员变量指针很奇怪,为0x00003001.最后检查的结果居然是这样的: 文件class1.h: class1 { int a; int b; } 文件: ...

  3. javascript学习笔记(一)

    学习书籍 Javascript高级程序设计 第3,4章 javascript数据类型 Undefined(undefined) Null(null,空指针) Boolean(true,false) N ...

  4. 利用PHP/MYSQL实现的简易微型博客(转)

    数据库:ly_php_base 表:ly_micro_blog(仅仅有一个表)字段:id,title,date,content,hits 文件: 文件 描述 default.php 默认主页.显示博文 ...

  5. Entity Framework中实现查询的几种方法

    在介绍几种方法前,献上一张图,希望图的作者不要追究我的盗图之过.本文的内容是我自学时的笔记,自学的内容来自网络.手打的代码,切不可直接复制过去用,会有好多错别字什么的. Entity SQL 类似于S ...

  6. 如何安装,卸载eclipse中的ADT,或者更新ADT

    昨天手贱把Android的SDK升级了,然后搞到ADT的版本不对应,然后从网上搜了一些办法~效果还可以,重新安装了一次ADT. 卸载ADT的方法,方法如下(我的Eclipse版本为3.5): 1.选择 ...

  7. git 分支的基本操作

    git分支的基本操作. 创建私有分支:     $git branch branchName commitID     $git checkout -b branchName commitID 注意: ...

  8. ios 调用相机后 view 下沉问题

    我只加了一句代码 现在不报错了  因为这个问题是随机性的  我也不太明白这个地方是怎么回事   我只是这样子做了  问题不出来了 if ([[[UIDevice currentDevice] syst ...

  9. OpenCV——使用ROI进行图像切割

    ROI(region of interest)——感兴趣区域. 1.用途 这个区域是图像分析所关注的重点.圈定这个区域,以便进行进一步的处理.而且,使用ROI指定 想读入的目标,可以减少处理时间,增加 ...

  10. hibernate中.hbm.xml和注解方式自动生成数据表的简单实例(由新手小白编写,仅适用新手小白)

    绝逼新手小白,so 请大神指点! 如果真的错的太多,错的太离谱,错的误导了其他小伙伴,还望大神请勿喷,大神请担待,大神请高抬贵嘴......谢谢. 好了,正题 刚接触ssh,今天在搞使用.hbm.xm ...