空调综合征有什么症状| 什么最重要| 支队长是什么级别| 多是什么结构的字| 性功能障碍吃什么药| 2029年属什么生肖| sport什么牌子| 心口窝疼挂什么科| pin什么意思| 摘胆对身体有什么影响| 眼睛有痣代表什么| 为什么腿老是抽筋| 什么是百分数| 桃园三结义是什么意思| 音容笑貌的意思是什么| 痛风吃什么水果| 禹五行属什么| essence什么意思| 身体缺钾是什么症状| 梦到自己长白头发是什么意思| 阴唇黑是什么原因| 平板和ipad有什么区别| 蛋白高是什么病| 四个横念什么| 女人代谢慢吃什么效果最快| 女人有卧蚕代表什么| 猪脚焖什么好吃| 收入是什么意思| 有什么软件可以赚钱| 灶性肠化是什么意思| 为什么会被鬼压床| 感冒吃什么好| 乙亥五行属什么| 孤家寡人什么意思| 李嘉诚是什么国籍| 滑石粉是什么东西| 海绵体充血不足吃什么药| 心眼小是什么意思| 厉鬼是什么意思| 炒什么菜适合拌面| 减肥期间可以吃什么水果| 脚背肿是什么原因| 下焦湿热是什么意思| 睡觉为什么磨牙| 猫咪喜欢什么颜色| 第三代试管是什么意思| 什么是阻生智齿| 每个月14号都是什么情人节| 一日三餐是什么生肖| 痔疮是什么感觉| 食用葡萄糖是什么| 疱疹用什么药可以根治| 满江红属于什么植物| 一什么饼干| 冰清玉洁是什么意思| 宫颈囊肿是什么意思| 79岁属什么| 美的不可方物是什么意思| 8月26号是什么星座| 哪吒为什么叫哪吒| 大小周休息是什么意思| 噫是什么意思| 鲁迅原名是什么| 宜入宅是什么意思| 辛亥革命是什么时候| 皈依有什么好处| 白热化阶段是什么意思| 内页是什么意思| 痛风吃什么好| 什么水果含糖量高| 三杯鸡的三杯是什么| river是什么意思| 慢性肠炎吃什么药效果好| 禁欲有什么好处| 拉尿分叉是什么原因| 开水冲鸡蛋有什么好处| 六月九号什么星座| 祛痣后应注意什么| 坐月子吃什么好| beauty是什么意思| 黄芪的功效是什么| 为什么现在不建议输液| 胃疼去医院挂什么科| 沉冤得雪是什么意思| 3月27号是什么星座| 什么是九宫格| 软饮料是指什么| ppada是什么牌子| cm医学上是什么意思| 棉绸是什么面料| 残疾证有什么补贴| 清朝什么时候建立| ala是什么| 三叉神经痛吃什么药效果最好| 癞蛤蟆长什么样| 大姨妈来了喝什么好| 本能反应是什么意思| 现在做什么最赚钱| 属马的跟什么属相最配| 梦见自己家盖房子是什么预兆| 胸口容易出汗是什么原因| 孕酮低有什么症状| 吃什么减脂肪| 妊娠是什么意思| 马兰头是什么菜| 窦骁父母是干什么的| 为什么女人阴唇会变大| 高粱是什么粮食| 折射率是什么意思| 黄体不足吃什么药| 血常规五项能检查出什么病| 有结石不能吃什么东西| 消化道出血有什么症状| 哺乳期可以喝什么饮料| 为什么会肚子痛| 市检察长是什么级别| 天梭手表什么档次| 小孩咳嗽不能吃什么食物| 风湿吃什么药好| 医是什么结构的字| 血小板下降是什么原因| 铁观音茶属于什么茶| poison是什么意思| 周天是什么意思| 拔罐颜色深浅代表什么| 纤尘是什么意思| 八面玲珑是什么意思| 六月二十六是什么日子| 心脏支架是什么材料做的| 摔纹皮是什么皮| 营养不良吃什么| 夏季摆摊卖什么好| 脚脖子肿是什么原因| 枸杞泡茶喝有什么功效| 江西庐山产什么茶| 吃什么增强免疫力最快| 血压高呕吐是什么征兆| 孩子为什么不愿意上学| supor是什么品牌| 大拇指旁边是什么指| 早晨起来口苦是什么原因| 结婚27年是什么婚| fans是什么意思| 腺样体是什么| 痱子涂什么药膏好| 关羽的刀叫什么名字| adhd是什么意思| 咖啡加牛奶叫什么| 女性绝经期在什么年龄是正常的| cll是什么意思| 腹胀是什么原因引起的| 小燕子吃什么| 血小板低有什么危害| 腹泻什么意思| 自渎是什么意思| emr是什么意思| 腿痛挂什么科| 阑尾切除后有什么影响和后遗症| 三七花泡水喝有什么功效和作用| 土字旁有什么字| 处女是什么| 山楂可以和什么一起泡水喝| 个性化是什么意思| 拔智齿后需要注意什么| 台湾人说什么语言| 屁股生疮是什么原因| 安五行属性是什么| 太阳穴疼什么原因| 甲亢吃什么药好得快| 7月属什么生肖| 梦到自己拔牙齿是什么预兆| 十一是什么意思| 酸野是什么| 小孩感冒发烧吃什么药| 他喵的什么意思| 电饭煲什么牌子好| 玉米蛇吃什么| 狗篮子什么意思| resp是什么| les是什么意思| 肠炎不能吃什么东西| 梨不能和什么一起吃| 甲功三项是检查什么| 药流后吃什么消炎药比较好| 猪肝不能和什么一起吃| 荷尔蒙是什么东西| 身上长小红点是什么原因| 壬水命是什么意思| 风湿免疫科是看什么病的| 真菌怕什么消毒液| 血涂片检查什么病| icu是什么| 马赛克什么意思| 眉头长痘痘是因为什么原因引起的| 为什么心脏会突然刺痛| 软卧代软座什么意思| 台湾什么时候收回| 什么什么不得| 下巴脖子长痘痘是什么原因| 肩周炎吃什么药好得快| 舌头凉凉的是什么原因| 结膜炎是什么原因引起的| 避孕套长什么样| 66岁属什么| 中国信什么教| touch是什么意思| 一什么招牌| 转念是什么意思| 为什么糙米越吃血糖越高| 什么叫密度| 做肠胃镜挂什么科| 8月23号是什么星座| 贫血吃什么水果补血最快| 滑膜增厚是什么意思| 7月14日什么星座| 人活一辈子到底为了什么| 为什么指甲会凹凸不平| 暗语是什么意思| 龙鱼吃什么| 骨膜炎是什么症状| 尿性什么意思| 熊掌有什么功效与作用| 右边偏头痛是什么原因| 第六感是什么意思| 体检前一天要注意什么| 汪小菲什么星座| 医学五行属什么| 寻麻疹吃什么药| cd3cd4cd8都代表什么| 70年是什么婚| 额头冒痘是什么原因| 为什么会得霉菌性阴道炎| 糖尿病人不能吃什么水果| 什么的嗓音| 大黄和芒硝混合外敷有什么作用| 什么叫阳虚| 什么分什么裂| 羊肉和什么食物相克| 七月二十九是什么星座| 鸽子是什么生肖| 十岁女孩喜欢什么礼物| halloween是什么意思| 兄弟是什么生肖| 早醒是什么原因| 什么是免疫组化| 泉州有什么好吃的| 抗体是指什么| 什么叫指标到校| 梦见摘枣吃枣是什么意思| 梦见锁门是什么意思| csw是什么意思| 恐龙什么时候灭绝的| 五代十国是什么意思| 打完耳洞不能吃什么| 吃鹅蛋对孕妇有什么好处| 赤小豆是什么| 为什么这么热| 休学需要什么条件| 颈椎脑供血不足吃什么药| 待我长发及腰时下一句是什么| 流口水是什么原因引起的| 梅雨季节什么时候结束| 什么花不用浇水| 叔叔的女儿叫什么| 百度Jump to content

《福建宣传教育》2015年第10期(总第二十四期)

From Wikipedia, the free encyclopedia
百度   从近两周的表现看,美国方面对协定的达成展现出极大的热情,预计在墨西哥大选前,NAFTA将会取得良好的进展。

In computer programming, thread-local storage (TLS) is a memory management method that uses static or global memory local to a thread. The concept allows storage of data that appears to be global in a system with separate threads.

Many systems impose restrictions on the size of the thread-local memory block, in fact often rather tight limits. On the other hand, if a system can provide at least a memory address (pointer) sized variable thread-local, then this allows the use of arbitrarily sized memory blocks in a thread-local manner, by allocating such a memory block dynamically and storing the memory address of that block in the thread-local variable. On RISC machines, the calling convention often reserves a thread pointer register for this use.

Usage

[edit]

While the use of global variables is generally discouraged in modern programming, some older operating systems such as UNIX were originally designed for uniprocessor hardware and often use global variables to store important values. An example is the errno used by many functions of the C library. On a modern machine, where multiple threads may be modifying the errno variable, a call of a system function on one thread may overwrite the value previously set by a call of a system function on a different thread, possibly before following code on that different thread could check for the error condition. The solution is to have errno be a variable that looks as if it is global, but is physically stored in a per-thread memory pool, the thread-local storage.

A second use case would be multiple threads accumulating information into a global variable. To avoid a race condition, every access to this global variable would have to be protected by a mutex. Instead, each thread might accumulate into a thread-local variable, thereby eliminating any possibility of a race condition, thereby removing the need for locking. The threads then only have to synchronise a final accumulation from their own thread-local variable into a single global variable.

Windows implementation

[edit]

The application programming interface (API) function TlsAlloc can be used to obtain an unused TLS slot index; the TLS slot index will then be considered 'used'.

The TlsGetValue and TlsSetValue functions are then used to read and write a memory address to a thread-local variable identified by the TLS slot index. TlsSetValue only affects the variable for the current thread. The TlsFree function can be called to release the TLS slot index.

There is a Win32 Thread Information Block for each thread. One of the entries in this block is the thread-local storage table for that thread.[1] Each call of TlsAlloc returns a unique index into this table. Each thread can independently use TlsSetValue(index, value) and obtain the specified value via TlsGetValue(index), because these set and look up an entry in the thread's own table.

Apart from TlsXxx function family, Windows executables can define a section which is mapped to a different page for each thread of the executing process. Unlike TlsXxx values, these pages can contain arbitrary and valid addresses. These addresses, however, are different for each executing thread and therefore should not be passed to asynchronous functions (which may execute in a different thread) or otherwise passed to code which assume that a virtual address is unique within the whole process. TLS sections are managed using memory paging and its size is quantized to a page size (4kB on x86 machines). Such sections may only be defined inside a main executable of a program - DLLs should not contain such sections, because they are not correctly initialized when loading with LoadLibrary.

Pthreads implementation

[edit]

In the Pthreads API, memory local to a thread is designated with the term Thread-specific data.

The functions pthread_key_create and pthread_key_delete are used respectively to create and delete a key for thread-specific data. The type of the key is explicitly left opaque and is referred to as pthread_key_t. This key can be seen by all threads. In each thread, the key can be associated with thread-specific data via pthread_setspecific. The data can later be retrieved using pthread_getspecific.

In addition pthread_key_create can optionally accept a destructor function that will automatically be called at thread exit, if the thread-specific data is not NULL. The destructor receives the value associated with the key as parameter so it can perform cleanup actions (close connections, free memory, etc.). Even when a destructor is specified, the program must still call pthread_key_delete to free the thread-specific data at process level (the destructor only frees the data local to the thread).

Language-specific implementation

[edit]

Apart from relying on programmers to call the appropriate API functions, it is also possible to extend the programming language to support thread local storage (TLS).

C and C++

[edit]

In C11, the keyword _Thread_local is used for defining thread-local variables. The header <threads.h>, if supported, defines thread_local as a synonym for that keyword. Example usage:

#include <threads.h>
thread_local int foo = 0;

In C11, <threads.h> also defines a number of functions for retrieving, changing, and destructing a thread-local storage, using names starting with tss_. In C23, thread_local itself becomes a keyword.[2]

C++11 introduces the thread_local[3] keyword which can be used in the following cases

  • Namespace level (global) variables
  • File static variables
  • Function static variables
  • Static member variables

Aside from that, various compiler implementations provide specific ways to declare thread-local variables:

On Windows versions before Vista and Server 2008, __declspec(thread) works in DLLs only when those DLLs are bound to the executable, and will not work for those loaded with LoadLibrary() (a protection fault or data corruption may occur).[10]

Common Lisp and other dialects

[edit]

Common Lisp provides a feature called dynamically scoped variables.

Dynamic variables have a binding which is private to the invocation of a function and all of the children called by that function.

This abstraction naturally maps to thread-specific storage, and Lisp implementations that provide threads do this. Common Lisp has numerous standard dynamic variables, and so threads cannot be sensibly added to an implementation of the language without these variables having thread-local semantics in dynamic binding.

For instance the standard variable *print-base* determines the default radix in which integers are printed. If this variable is overridden, then all enclosing code will print integers in an alternate radix:

;;; function foo and its children will print
;; in hexadecimal:
(let ((*print-base* 16)) (foo))

If functions can execute concurrently on different threads, this binding has to be properly thread-local, otherwise each thread will fight over who controls a global printing radix.

D

[edit]

In D version 2, all static and global variables are thread-local by default and are declared with syntax similar to "normal" global and static variables in other languages. Global variables must be explicitly requested using the shared keyword:

int threadLocal;  // This is a thread-local variable.
shared int global;  // This is a global variable shared with all threads.

The shared keyword works both as the storage class, and as a type qualifiershared variables are subject to some restrictions which statically enforce data integrity.[13] To declare a "classic" global variable without these restrictions, the unsafe __gshared keyword must be used:[14]

__gshared int global;  // This is a plain old global variable.

Java

[edit]

In Java, thread-local variables are implemented by the ThreadLocal class object.[15] ThreadLocal holds variable of type T,[15] which is accessible via get/set methods. For example, ThreadLocal variable holding Integer value looks like this:

private static final ThreadLocal<Integer> myThreadLocalInteger = new ThreadLocal<Integer>();

At least for Oracle/OpenJDK, this does not use native thread-local storage in spite of OS threads being used for other aspects of Java threading. Instead, each Thread object stores a (non-thread-safe) map of ThreadLocal objects to their values (as opposed to each ThreadLocal having a map of Thread objects to values and incurring a performance overhead).[16]

.NET languages: C# and others

[edit]

In .NET Framework languages such as C#, static fields can be marked with the ThreadStatic attribute:[17]:?898?

class FooBar
{
    [ThreadStatic]
    private static int _foo;
}

In .NET Framework 4.0 the System.Threading.ThreadLocal<T> class is available for allocating and lazily loading thread-local variables.[17]:?899?

class FooBar
{
    private static System.Threading.ThreadLocal<int> _foo;
}

Also an API is available for dynamically allocating thread-local variables.[17]:?899–890?

Object Pascal

[edit]

In Object Pascal (Delphi) or Free Pascal the threadvar reserved keyword can be used instead of 'var' to declare variables using the thread-local storage.

var
   mydata_process: integer;
threadvar
   mydata_threadlocal: integer;

Objective-C

[edit]

In Cocoa, GNUstep, and OpenStep, each NSThread object has a thread-local dictionary that can be accessed through the thread's threadDictionary method.

NSMutableDictionary *dict = [[NSThread currentThread] threadDictionary];
dict[@"A key"] = @"Some data";

Perl

[edit]

In Perl threads were added late in the evolution of the language, after a large body of extant code was already present on the Comprehensive Perl Archive Network (CPAN). Thus, threads in Perl by default take their own local storage for all variables, to minimise the impact of threads on extant non-thread-aware code. In Perl, a thread-shared variable can be created using an attribute:

use threads;
use threads::shared;

my $localvar;
my $sharedvar :shared;

PureBasic

[edit]

In PureBasic thread variables are declared with the keyword Threaded.

Threaded Var

Python

[edit]

In Python version 2.4 or later, local class in threading module can be used to create thread-local storage.

import threading
mydata = threading.local()
mydata.x = 1

Multiple instances of local class can be created to store different sets of variables.[18] Thus, it is not a singleton.

Ruby

[edit]

Ruby can create/access thread-local variables using []=/[] methods:

Thread.current[:user_id] = 1

Rust

[edit]

Thread-local variables can be created in Rust using the thread_local! macro provided by the Rust standard library:

use std::cell::RefCell;
use std::thread;

thread_local!(static FOO: RefCell<u32> = RefCell::new(1));

FOO.with(|f| {
    assert_eq!(*f.borrow(), 1);
    *f.borrow_mut() = 2;
});

// each thread starts out with the initial value of 1, even though this thread already changed its copy of the thread local value to 2
let t = thread::spawn(move || {
    FOO.with(|f| {
        assert_eq!(*f.borrow(), 1);
        *f.borrow_mut() = 3;
    });
});

// wait for the thread to complete and bail out on panic
t.join().unwrap();

// original thread retains the original value of 2 despite the child thread changing the value to 3 for that thread
FOO.with(|f| {
    assert_eq!(*f.borrow(), 2);
});

See also

[edit]
  • OpenMP — Another shared-memory multiprocessing facility which supports per-thread storage via "Data sharing attribute clauses" (see under §Clauses)

References

[edit]
  1. ^ Pietrek, Matt (May 2006). "Under the Hood". Microsoft Systems Journal. 11 (5). Archived from the original on 9 September 2010. Retrieved 6 April 2010.
  2. ^ "Concurrency support library - cppreference.com". en.cppreference.com.
  3. ^ Section 3.7.2 in C++11 standard
  4. ^ "C-Compiler Information Specific to Sun's Implementation". C User's Guide Sun Studio 8. 2004. 2.3 Thread Local Storage Specifier.
  5. ^ "XL C/C++ compilers". August 2010. Thread-local storage (TLS). Archived from the original on 11 April 2011.
  6. ^ "Thread-Local Storage". GCC 3.3.1 Manual. 2003.
  7. ^ "LLVM 2.0 Release Notes". 23 May 2007. llvm-gcc Improvements.
  8. ^ "Clang Language Extensions - Clang 3.8 documentation". Introduction. This document describes the language extensions provided by Clang. In addition to the language extensions listed here, Clang aims to support a broad range of GCC extensions. Please see the GCC manual for more information on these extensions.
  9. ^ "Intel? C++ Compiler 8.1 for Linux Release Notes For Intel IA-32 and Itanium? Processors" (PDF). 2004. Thread-local Storage. Archived from the original (PDF) on 19 January 2015.
  10. ^ a b Visual Studio 2003: "Thread Local Storage (TLS)". Microsoft Docs. 5 June 2017.
  11. ^ Intel C++ Compiler 10.0 (windows): Thread-local storage
  12. ^ "Attributes in Clang - Clang 3.8 documentation". thread.
  13. ^ Alexandrescu, Andrei (6 July 2010). "Chapter 13 - Concurrency". The D Programming Language. InformIT. p. 3. Retrieved 3 January 2014.
  14. ^ Bright, Walter (12 May 2009). "Migrating to Shared". dlang.org. Retrieved 3 January 2014.
  15. ^ a b Bloch 2018, p. 151-155, §Item 33: Consider typesafe heterogeneous containers.
  16. ^ "How is Java's ThreadLocal implemented under the hood?". Stack Overflow. Stack Exchange. Retrieved 27 December 2015.
  17. ^ a b c Albahari 2022.
  18. ^ "cpython/Lib/_threading_local.py at 3.12 · python/cpython". GitHub. Retrieved 25 October 2023.

Bibliography

[edit]
  • Albahari, Joseph (2022). C# 10 in a Nutshell (First ed.). O'Reilly. ISBN 978-1-098-12195-2.
  • Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.


[edit]
甲钴胺片有什么副作用 夏天都有什么花 肾积水有什么症状表现 1996年出生属什么 子宫增大是什么原因造成的
单位时间是什么意思 吃什么补气血 水乳什么牌子好用 蔚字五行属什么 别墅是什么意思
爱情是什么 优衣库属于什么档次 升血压吃什么药 cm3是什么单位 检查免疫力都需要化验什么项目
生理期吃什么比较好 什么咖啡好喝 乔治白属于什么档次 精梳棉是什么面料 onemore是什么牌子
冬瓜什么时候种植最好hcv9jop6ns2r.cn 门当户对指的是什么hcv8jop0ns2r.cn 特派员是什么级别hcv8jop2ns4r.cn 月经期间吃西瓜有什么影响clwhiglsz.com 大枣和红枣有什么区别wuhaiwuya.com
斑鱼是什么鱼hcv9jop4ns4r.cn 蝙蝠飞到家里是什么预兆hkuteam.com 牟作为姓氏时读什么liaochangning.com 寒露是什么季节hcv7jop9ns0r.cn 妊高症是什么意思hcv8jop8ns7r.cn
为什么会有牙结石hcv8jop4ns7r.cn 申时左眼跳是什么预兆hcv8jop8ns4r.cn 容易静电的人说明什么hcv9jop5ns2r.cn 大材小用是什么生肖hcv9jop3ns2r.cn 什么酒最贵hcv8jop7ns1r.cn
恐惧是什么意思hcv8jop1ns0r.cn 护理专业出来能干什么hcv8jop7ns9r.cn 狗到家里是什么预兆hcv9jop3ns2r.cn 血小板是什么意思hcv9jop5ns1r.cn 金丝雀是什么意思hcv9jop4ns9r.cn
百度