Thread-local storage

QS-Informatik
Beteilige dich an der Diskussion!
Dieser Artikel wurde wegen inhaltlicher Mängel auf der Qualitätssicherungsseite der Redaktion Informatik eingetragen. Dies geschieht, um die Qualität der Artikel aus dem Themengebiet Informatik auf ein akzeptables Niveau zu bringen. Hilf mit, die inhaltlichen Mängel dieses Artikels zu beseitigen, und beteilige dich an der Diskussion! (+)

Thread-local storage (TLS) ist Speicherplatz, der für jeden Thread eines mehrläufigen Prozesses Daten threadspezifisch speichern kann.[1] Variablen mit dem Attribut TLS erhalten für jeden Thread automatisch eine eigene Instanz. Der Compiler erzeugt weiterhin automatisch Code, der Zugriffe auf genau die dem Thread zugeordnete Kopie des TLS erlaubt. TLS kann dazu dienen, Daten vor unbeabsichtigten Änderungen durch andere Threads zu schützen, oder Race Conditions zu verhindern.

Unter Windows/IA-32 und Windows x86 bzw. x64 z. B. erfolgt der Zugriff über das Segment-Register FS (x86) bzw. GS (x64), das für jeden Thread auf den sogenannten Thread Environment Block verweist.

Codebeispiel für GCC mithilfe des Attributs __thread:[2]

__thread int i;
extern __thread struct state s;
static __thread char *p;

Unter Visual C++ mithilfe des Attributs __declspec(thread):

__declspec(thread) int i;
extern __declspec(thread) struct state s;
static __declspec(thread) char *p;

Seit C++11 existiert für Deklarationen das Schlüsselwort thread_local als einheitliche Alternative zu diesen GCC- bzw. MSVC-Attributen.[3]

TLS ist für die Architekturen IA-32, x86-64, SPARC, SuperH, Alpha und S390 sowie unter ARM-Prozessoren verfügbar.[2][4]

Einzelnachweise

  1. Tyler Whitney et al.: Threadlokaler Speicher (TLS). In: learn.microsoft.com. 26. September 2022, abgerufen am 28. Februar 2023.
  2. a b Ulrich Drepper: ELF Handling For Thread-Local Storage. (PDF; 480 kB) In: akkadia.org. 22. August 2013, abgerufen am 3. August 2014 (englisch, Version 0.21).
  3. Storage class specifiers. In: cppreference.com. 23. Juni 2021, abgerufen am 20. Juli 2021 (englisch).
  4. RealView Compilation Tools Compiler User Guide > Thread-local storage. In: developer.arm.com. Abgerufen am 20. Juli 2021 (englisch).

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.