CPP2019SSF28: Unterschied zwischen den Versionen

Aus Verteilte Systeme - Wiki
Zur Navigation springen Zur Suche springen
Zeile 20: Zeile 20:
 
=== Motivation für die Einführung ===
 
=== Motivation für die Einführung ===
 
Implementing the thread local storage method in c++ 98 is only possible by using
 
Implementing the thread local storage method in c++ 98 is only possible by using
  +
third party libraries. With C++11 and std::thread we don't have to rely on third parties libraries, this make our code more portable.
third party libraries.
 
   
 
=== Vorherige Lösungsansätze ===
 
=== Vorherige Lösungsansätze ===

Version vom 14. August 2019, 16:00 Uhr

Feature: thread_local

  • Eingeführt in: C++11

Syntax

thread_local varType varName;
thread_local unsigned int counter;
thread_local std::string s = "getting schwifty";
...

Use Cases

thread_local Entities have thread storage duration. This mean that every single thread in a program has its own copy of its thread_local Entity. thread_local Entities are implicit static and get destroyed upon thread exit.

The best way to avoid data races is not sharing the data between threads. By using thread_local variables, each thread will have its own copy of the data, and this data can only be written or reading by its own thread.

Motivation für die Einführung

Implementing the thread local storage method in c++ 98 is only possible by using third party libraries. With C++11 and std::thread we don't have to rely on third parties libraries, this make our code more portable.

Vorherige Lösungsansätze

Warnhinweise

  • Thead Local variables can't be acceded by other threads, UNLESS the programmer gives a pointer to the thread local variable.
  • Thread local cannot be used on function declarations or definitions.

Code-Beispiele