thread-local-storage

Why may thread_local not be applied to non-static data members and how to implement thread-local non-static data members?

有些话、适合烂在心里 提交于 2019-12-10 14:36:34
问题 Why may thread_local not be applied to non-static data members? The accepted answer to this question says: "There is no point in making non-static structure or class members thread-local." Honestly, I see many good reasons to make non-static data members thread-local. Assume we have some kind of ComputeEngine with a member function computeSomething that is called many times in succession. Some of the work inside the member function can be done in parallel. To do so, each thread needs some

Dynamic TLS in C++11

梦想与她 提交于 2019-12-09 18:20:44
问题 I'm writing a C++11 class Foo, and I want to give each instance its own thread-local storage of type Bar. That is to say, I want one Bar to be allocated per thread and per Foo instance. If I were using pthreads, Foo would have a nonstatic member of type pthread_key_t, which Foo's constructor would initialize with pthread_key_create() and Foo's destructor would free with pthread_key_delete(). Or if I were writing for Microsoft Windows only, I could do something similar with TlsAlloc() and

thread-local storage overhead

人盡茶涼 提交于 2019-12-09 17:46:29
问题 Assume there is some not-reentrant function that uses global variables: int i; void foo(void){ /* modify i */ } And then, I want to use this function in multithreaded code, so I can change code this way: void foo(int i){ /* modify i */ } or, by using gcc __thread specifier, more simplier: __thread int i; void foo(void){ /* modify i */ } Advantages of the last is that I don't need to change another code which call foo(). My questions is, how much overhead of thread-local storage is? Is there

Does Go have something like ThreadLocal from Java?

不想你离开。 提交于 2019-12-09 14:53:54
问题 I use Go and Gin to setup my website and want to know the database access time. I use goroutine so if don't use something like thread-local, I must change almost every function to do it. Does Go have a good way to do it? 回答1: The Go runtime and standard libraries do not provide goroutine local storage or a goroutine identifier that can be used to implement goroutine local storage. The third party gls package implements goroutine local storage in an interesting way. Some find this package

TPL Data Flow Thread Local Data

≡放荡痞女 提交于 2019-12-08 13:17:16
问题 Is there a good way to pass thread local data into an ActionBlock, such that if you specify MaxDegreeOfParallelization in its DataFlowExecutionOptions to be > 1, then each task that executes the action will have its own thread local data? Here is some of my code that will perhaps clarify what I want to do: var options = new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = 12 }; ActionBlock<int> actionBlock = new ActionBlock<int>(PerformAction, options); List<int> resultsList = new

thread-local static uniform_int_distribution, will it be recreated for different sets of min, max?

岁酱吖の 提交于 2019-12-08 12:45:32
In this answer on stackoverflow (answer #2) @remyabel recommends this template: #include <random> std::mt19937& prng_engine() { thread_local static std::random_device rd{}; thread_local static std::mt19937 engine{rd()}; // Or you can replace the two previous lines with: //thread_local static std::mt19937 // prng{std::random_device{}()}; return engine; } template<typename T> T getRandomNumberBetween(T Min, T Max) { thread_local static std::uniform_int_distribution<T> dist{Min, Max}; return dist(prng_engine()); } Will dist be created only once for give set of { T, Min, Max } in thread? If I call

Code sequences for TLS on ARM

旧街凉风 提交于 2019-12-07 04:38:04
问题 The ELF Handling For Thread-Local Storage document gives assembly sequences for the various models (local exec/initial exec/general dynamic) for various architectures. But not ARM -- is there anywhere I can see such code sequences for ARM? I'm working on a compiler and want to generate code that will operate properly with the platform linkers (both program and dynamic). For clarity, let's assume an ARMv7 CPU and a pretty new kernel and glibc (say 3.13+ / 2.19+), but I'd also be interested in

C++11: GCC 4.8 static thread_local std::unique_ptr undefined reference

血红的双手。 提交于 2019-12-06 17:54:28
问题 I need to store a unique pointer for every thread that will be accessed through a macro. I thought I should solve this with a singleton and static thread_local std::unique_ptr objects. Here is a simplified version of the code: main.cpp #include <thread> #include <vector> #include <iostream> #include <mutex> using namespace std; #include "yay.hpp" mutex coutMutex; void yay(int id) { int* yayPtr = getYay(); // I know this is bad coutMutex.lock(); cout << "Yay nr. " << id << " address: " <<

Is it legal to initialize a thread_local variable in the destructor of a global variable?

北城以北 提交于 2019-12-06 17:45:21
问题 This program: #include <iostream> struct Foo { Foo() { std::cout << "Foo()\n"; } ~Foo() { std::cout << "~Foo()\n"; } }; struct Bar { Bar() { std::cout << "Bar()\n"; } ~Bar() { std::cout << "~Bar()\n"; thread_local Foo foo; } }; Bar bar; int main() { return 0; } Prints Bar() ~Bar() Foo() for me (GCC 6.1, Linux, x86-64). ~Foo() is never called. Is that the expected behaviour? 回答1: The Standard does not cover this case; the strictest reading would be that it is legal to initialize a thread_local

Pthread Thread-Local-Singleton, when to release the TLS Key?

强颜欢笑 提交于 2019-12-06 01:54:08
问题 I implemented a kind of "thread local singleton" using pthread TLS, and i wondered how (and when) i could possibly delete the pthread_key_t in this case, because as it is now, the memory used by the TLS key will never be free'd. The intended usage of this is to let a class A derive from ThreadLocalSingleton<A> which makes A a thread local singleton, assuming that A has only private constructors and ThreadLocalSingleton<A> is a friend of A. Oh and also - do you see any problems with that