线程特定数据VS线程本地存储 [英] Thread Specific Data vs Thread Local Storage

查看:225
本文介绍了线程特定数据VS线程本地存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过Kerrisk的 Linux的编程接口:一个Linux和UNIX系统编程手册,在线程第31章。本章包括线程特定数据(第31.3.4)和线程本地存储(31.4节)。的主题是覆盖在663-669页。

I've read Kerrisk's The Linux Programming Interface: A Linux and UNIX System Programming Handbook, Chapter 31 on Threads. The chapter include Thread Specific Data (Section 31.3.4) and Thread Local Storage (Section 31.4). The topics were covered on pages 663-669.

主题特定数据( pthread_key_create pthread_setspecific pthread_getspecific ,和朋友)看起来更强大,但似乎有点比较麻烦使用,并显示使用内存管理器更频繁。

Thread Specific Data (pthread_key_create, pthread_setspecific, pthread_getspecific, and friends) looks more powerful, but appears to be a little more cumbersome to use, and appears to use the memory manager more frequently.

线程局部存储( __线程静态和全局声明)看起来,因为有点不太强大的有限的编译时间,但它似乎更容易使用,并出现留出了内存管理器在运行时。

Thread Local Storage (__thread on static and global declarations) looks a little less powerful since its limited to compile time, but its appears to be easier to use, and appears to stay out of the memory manager at runtime.

我可能是错的运行时内存管理器,因为有可能是调用 pthread_key_create 当它遇到 __线程幕后code 变量。

I could be wrong about the runtime memory manager since there could be code behind the scenes that calls pthread_key_create when it encounters __thread variables.

Kerrisk没有提供这两种策略的比较/相反,他并没有对当其时在特定情况下使用的建议。

Kerrisk did not offer a compare/contrast of the two strategies, and he did not make a recommendation on when to use which in a given situation.

要添加上下文的问题:我评价一个第三方库。库使用全局变量,确实的的使用锁,我想在多线程程序中使用它。该方案采用线程,以尽量减少网络延迟。

To add context to the question: I'm evaluating a 3rd party library. The library uses globals, does not utilize locking, and I want to use it in a multi-threaded program. The program uses threading to minimize network latencies.

有一个倒手的赢家?或者正在使用一种或另一种是保证有不同的方案?

Is there a hands down winner? Or are there different scenarios that warrant using one or the other?

推荐答案

ptread_key_create 和朋友们大年纪了,更多的系统,因此支持。

The ptread_key_create and friends are much older, and thus supported on more systems.

__线程是一个相对较新,一般的更方便的使用,(的根据是支持大多数POSIX系统维基百科)仍然件事:工作室的Solaris C / C ++,IBM XL C / C ++,GNU C,锵和英特尔C ++编译器(Linux系统)。

The __thread is a relative newcomer, is generally much more convenient to use, and (according to Wikipedia) is supported on most POSIX systems that still matter: Solaris Studio C/C++, IBM XL C/C++, GNU C, Clang and Intel C++ Compiler (Linux systems).

__线程也有显著的优势,它是从信号处理程序可以使用(使用外 __线程的dlopen 编辑共享库,请参阅本错误),因为它的使用不涉及的malloc (具有相同的除外)。

The __thread also has a significant advantage that it is usable from signal handlers (with the exception of using __thread from dlopened shared library, see this bug), because its use does not involve malloc (with the same exception).

这篇关于线程特定数据VS线程本地存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆