18

I read that there was a new keyword in C++: it's __thread from what I've read.

All I know is that it's a keyword to be used like the static keyword but I know nothing else. Does this keyword just mean that, for instance, if a variable were declared like so:

__thread int foo;

then anything to do with that variable will be executed with a new thread?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
luckyl
  • 183
  • 1
  • 1
  • 7

4 Answers4

27

It's thread_local, not __thread. It's used to define variables which has storage duration of the thread.

thread_local is a new storage duration specifier added in C++0x. There are other storage duration : static, automatic and dynamic.

From this link:

thread local storage duration (C++11 feature). The variable is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the variable. Only variables declared thread_local have this storage duration.


I think the introduction of this keyword was made possible by introducing a standardized memory model in C++0x:

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • whoa, the answer looks completely different than it did 3 minutes ago. – Mooing Duck Aug 12 '11 at 23:14
  • 1
    "`thread_local` is a new storage duration specifier added in C++03": What nonsense is this?! – Lightness Races in Orbit Aug 15 '11 at 02:04
  • @Tomalak: You know that it was not a "nonsense" but a "typo", don't you? – Nawaz Aug 15 '11 at 02:32
  • Unfortunately Apple still doesn't want you using it: http://stackoverflow.com/questions/28094794/why-does-apple-clang-disallow-c11-thread-local-when-official-clang-supports – Alex Jansen Nov 04 '15 at 19:59
  • 4
    __thread is supported on GNU, clang and more. It was available before thread_local... they are not equivalent and both are supported. the difference is that thread_local uses lazy initialization to initialize the variable in only threads that access it. __thread does not initialize at all and you must manually initialize it per thread. thread_local thus has an overhead per access and __thread does not. Apple's compilers disable thread_local and not thread because of this inefficiency, Although __thread is not available on all compilers, __thread is available with GNU tools. – Ben Nov 27 '15 at 00:00
  • I've seen `__thread` used on functions. What's the meaning of it there? – PoVa Feb 28 '18 at 06:13
22

From the Wikipedia article on "Thread-local storage":

Thread-local storage (TLS) is a computer programming method that uses static or global memory local to a thread.

This is sometimes needed because normally all threads in a process share the same address space, which is sometimes undesirable.

And:

C++0x introduces the thread_local keyword. Aside that, various C++ compiler implementations provide specific ways to declare thread-local variables:

Sun Studio C/C++, IBM XL C/C++, GNU C and Intel C/C++ (Linux systems) use the syntax:

    __thread int number;

Visual C++, Intel C/C++ (Windows systems), Borland C++ Builder and Digital Mars C++ use the syntax:

    __declspec(thread) int number;

Borland C++ Builder also supports the syntax:

    int __thread number;

So, whilst __thread does exist in practice and on some systems, thread_local is the new, official, C++0x keyword that does the same thing.

Prefer it to non-standard __thread whenever you have access to C++0x.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
5

The keyword is called thread_local. It means that each thread has its own version of that variable.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
2

No, it does not mean that "anything to do with that variable will be executed with a new thread". It means that there will be a copy of the variable for each thread that exists, and each thread can only see its own copy of the variable.

hmakholm left over Monica
  • 23,074
  • 3
  • 51
  • 73