thread-local-storage

_Thread_local storage class specifier in C?

五迷三道 提交于 2019-12-02 01:28:48
问题 I read a note in the book C How to Program 7th about some new standard C storage class named _Thread_local : The new C standard adds storage class specifier _Thread_local , which is beyond this book's scope. I looked for it in Google and here but nothing show up. Could someone please provide me some link about it? 回答1: Variables marked with _Thread_local are given "thread" storage duration -- that is, they are allocated when a thread begins, and deallocated when the thread ends. Such

Why is a static thread_local object in C++ constructed twice?

爷,独闯天下 提交于 2019-12-01 16:26:16
This code: #include <iostream> #include <thread> #include <mutex> struct Singl{ Singl(Singl const&) = delete; Singl(Singl&&) = delete; inline static thread_local bool alive = true; Singl(){ std::cout << "Singl() " << std::this_thread::get_id() << std::endl; } ~Singl(){ std::cout << "~Singl() " << std::this_thread::get_id() << std::endl; alive = false; } }; static auto& singl(){ static thread_local Singl i; return i; } struct URef{ ~URef(){ const bool alive = singl().alive; std::cout << alive << std::endl; } }; int main() { std::thread([](){ singl(); static thread_local URef u; }).join();

What are the real ELF TLS ABI requirements for each cpu arch?

核能气质少年 提交于 2019-12-01 03:49:29
Ulrich Drepper's paper on thread-local storage outlines the TLS ABI for several different cpu architectures, but I'm finding it insufficient as a basis for implementing TLS for two reasons: It omits a number of important archs like ARM, MIPS, etc. (while including a bunch of completely-irrelevant ones like Itanium) More importantly, it mixes a lot of implementation details with ABI, so that it's hard to tell which properties are required for interoperability, and which are just aspects of his implementation. As an example, the only actual ABI requirements for i386 are: %gs:0 points to a

Addresses of Thread Local Storage Variables

故事扮演 提交于 2019-11-30 21:53:50
OK, say that I have __thread int myVar; And I then pass &myVar from one thread to another ... If the data is truly "local", then the TLS storage of 1 thread may not be mapped into the other threads address space, and in fact, you could argue that it shouldn't be. This would result in a SIGSEGV or something. However, the system could just map the same address to a different page. Is this what Linux does with .tbss/.tdata? In that case, passing the address of a variable would give you the address of the wrong variable! You'd get your own local copy and not the copy you tried to pass. Or, is

Addresses of Thread Local Storage Variables

南笙酒味 提交于 2019-11-30 17:56:56
问题 OK, say that I have __thread int myVar; And I then pass &myVar from one thread to another ... If the data is truly "local", then the TLS storage of 1 thread may not be mapped into the other threads address space, and in fact, you could argue that it shouldn't be. This would result in a SIGSEGV or something. However, the system could just map the same address to a different page. Is this what Linux does with .tbss/.tdata? In that case, passing the address of a variable would give you the

How to declare a variable as thread local portably?

谁说胖子不能爱 提交于 2019-11-30 13:08:39
C11 introduces the _Thread_local storage class specifier that can be used in combination with the static and extern storage class specifiers to declare a variable as thread local. The GNU C compiler suite implements a storage class specifier __thread with the same same semantics. Unfortunately I did not find any compiler (I tried gcc, clang and SUN studio) that actually implements the _Thread_local keywords. I currently use the following construct to declare a keyword thread_local : /* gcc doesn't know _Thread_local from C11 yet */ #ifdef __GNUC__ # define thread_local __thread #elif __STDC

Thread Specific Data vs Thread Local Storage

假装没事ソ 提交于 2019-11-29 07:45:56
问题 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. 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

How does the gcc `__thread` work?

ⅰ亾dé卋堺 提交于 2019-11-28 05:54:52
How is __thread in gcc implemented? Is it simply a wrapper over pthread_getspecific and pthread_setspecific ? With my program that uses the posix API for TLS, I'm kind of disappointed now seeing that 30% of my program runtime is spent on pthread_getspecific . I called it on the entry of each function call that needs the resource. The compiler doesn't seem to optimize out pthread_getspecific after inlining optimization. So after the functions are inlined the code is basically searching for the correct TLS pointer again and again to get the same pointer returned. Will __thread help me in this

Linux's thread local storage implementation

非 Y 不嫁゛ 提交于 2019-11-28 04:31:57
__thread Foo foo; How is "foo" actually resolved? Does the compiler silently replace every instance of "foo" with a function call? Is "foo" stored somewhere relative to the bottom of the stack, and the compiler stores this as "hey, for each thread, have this space near the bottom of the stack, and foo is stored as 'offset x from bottom of stack'"? It's a little complicated ( this document explains it in great detail), but it's basically neither. Instead the compiler puts a special .tdata section in the executable, which contains all the thread-local variables. At runtime, a new data section

How to allocate thread local storage?

牧云@^-^@ 提交于 2019-11-28 03:05:54
I have a variable in my function that is static, but I would like it to be static on a per thread basis. How can I allocate the memory for my C++ class such that each thread has its own copy of the class instance? AnotherClass::threadSpecificAction() { // How to allocate this with thread local storage? static MyClass *instance = new MyClass(); instance->doSomething(); } This is on Linux. I'm not using C++0x and this is gcc v3.4.6. #include <boost/thread/tss.hpp> static boost::thread_specific_ptr< MyClass> instance; if( ! instance.get() ) { // first time called by this thread // construct test