线程安全

在UNIX的多线程下用到errno的时候,要留意下了,否则无法得到正确的errno。
在AIX下,编译的时候需要加入-D_THREAD_SAFE
在solaris下,编译的时候需要加入 -D_REENTRANT
在hp-ux下,编译的时候 需要加入  -D_REENTRANT
在linux下,不存在任何问题,可以放心大胆的使用
通过这个问题,还学到一招,查看include头文件,我想在某些时候会起到些作用。
比如,查看AIX下的/usr/include/errno.h文件,会发现这样的声明:
#if defined(_THREAD_SAFE) || defined(_THREAD_SAFE_ERRNO)
/*
* Per thread errno is provided by the threads provider. Both the extern int
* and the per thread value must be maintained by the threads library.
*/
extern   int      *_Errno( void );
#define errno    (*_Errno())
#else
extern int errno;
#endif   /* _THREAD_SAFE || _THREAD_SAFE_ERRNO */
所以不言而喻了,需要加入_THREAD_SAFE 或者 _THREAD_SAFE_ERRNO
然后查看solaris的/usr/include/errno.h就不是这两个宏了。
联想有时候编译某些开源代码的时候,会报一些错误,说找不到某些函数,有可能就是某些宏没有定义,查一查相关的inclue文件,没准就编过去了。

posted on 2012-08-07 17:47  山本二十八  阅读(243)  评论(0编辑  收藏  举报

导航