+ -
当前位置:首页 → 问答吧 → _REENTRANT, _THREAD_SAFE 解释说明里面的英文没看明白。

_REENTRANT, _THREAD_SAFE 解释说明里面的英文没看明白。

时间:2010-06-27

来源:互联网

[Macro]
_REENTRANT
                                                                                     [Macro]
_THREAD_SAFE
     If you define one of these macros, reentrant versions of several functions get declared.
     Some of the functions are specified in POSIX.1c but many others are only available
     on a few other systems or are unique to GNU libc. The problem is the delay in the
     standardization of the thread safe C library interface.
     Unlike on some other systems, no special version of the C library must be used for
     linking. There is only one version but while compiling this it must have been specified
     to compile as thread safe.

gnu libc 的文档。 主要是第二段没看懂。 另外,这两个宏,在这里的作用, 我其实也没太明白, 哪里大大
能稍微解释下? 以线程安全的方式编译? 定义这两个宏,与没定义,具体的区别是?

作者: peidright   发布时间: 2010-06-27

不知道glibc是怎么弄的, 但是下面的做法, 也可以达到这个目标:


QUOTE:
no special version of the C library must be used for linking.



以malloc/free举例。

二进制的库不分"单线程/多线程"两种版本, 只有一个版本。
而且不导出"线程安全版本"的函数, 而只导出"非线程安全的函数":
  1. void* __malloc_st(size_t __bytes);
  2. void  __free_st(void* __ptr);
复制代码
同时, 导出两个函数, 用于获得c-heap背后的数据结构的锁:
  1. void __c_heap_lock(void);
  2. void __c_heap_unlock(void);
复制代码
无论客户需要哪种, 链接的都是同一个版本的二进制库。
是否线程安全的区别被编译进客户端的二进制代码中 :
  1. #ifdef C_HEAP_MT
  2. static inline void* malloc(size_t bytes) { return __malloc_st(bytes); }
  3. static inline void  free(void* ptr) { __free_st(ptr); }
  4. #else
  5. static inline void* malloc(size_t bytes) {
  6.       void* p;
  7.       __c_heap_lock();
  8.       p = __malloc_st(bytes);
  9.       __c_heap_unlock();
  10.       return p;
  11. }

  12. static inline void  free(void* ptr) {
  13.       __c_heap_lock();
  14.       __free_st(ptr);
  15.       __c_heap_unlock();
  16. }
复制代码

作者: OwnWaterloo   发布时间: 2010-06-27

大概明白了,是你说的这个意思,。。。。误判阿!。。。看球没有

作者: peidright   发布时间: 2010-06-27

这个…… 看不懂
至今连什么叫越位都不懂……

作者: OwnWaterloo   发布时间: 2010-06-27