Is using `std::get` on a `std::tuple` guaranteed to be thread-safe for different values of `I`?

Since std::get has no explicit statements in the specification about its data race properties, we fall back to the default behavior defined in [res.on.data.races]. Specifically, paragraphs 2 and 3 tell the story: A C++ standard library function shall not directly or indirectly access objects (1.10) accessible by threads other than the current thread unless the … Read more

Thread.sleep() VS Executor.scheduleWithFixedDelay()

You’re dealing with sleep times termed in tens of seconds. The possible savings by changing your sleep option here is likely nanoseconds or microseconds. I’d prefer the latter style every time, but if you have the former and it’s going to cost you a lot to change it, “improving performance” isn’t a particularly good justification. … Read more

When is a `thread_local` global variable initialized?

The standard allows for this behavior, although it doesn’t guarantee it. From 3.7.2/2 [basic.stc.thread]: A variable with thread storage duration shall be initialized before its first odr-use (3.2) and, if constructed, shall be destroyed on thread exit. It’s also possible that the objects are constructed at some other time (e.g. on program startup), as “before … Read more

Acquire/release semantics with 4 threads

You are thinking in terms of sequential consistency, the strongest (and default) memory order. If this memory order is used, all accesses to atomic variables constitute a total order, and the assertion indeed cannot be triggered. However, in this program, a weaker memory order is used (release stores and acquire loads). This means, by definition … Read more

what is the meaning of restrict in the function signature?

It’s something introduced in C99 which lets the compiler know that the pointer passed in there isn’t pointing to the same place as any other pointers in the arguments. If you give this hint to the compiler, it can do some more aggressive optimizations without breaking code. As an example, consider this function: int add(int … Read more