I'm using a custom implementation of coroutines in C++ (compiler g++, on ARM). The coroutines might migrate from one thread to another by calling a move_to_thread function (or other ways, but this will let me make my point). I'm oversimplifying, but it's kind of like this:
__thread int x = 0;
void f() {
x = 5;
// do some more work on current thread (thread 1, say)
move_to_thread(2);
// do more work, now on thread 2
int y = x; // with optimization, I'm getting the wrong x
}
The problem I'm having is that the work done before and after calling move_to_thread uses thread-local variables (using __thread
). When compiling with optimization, the code running on thread 2 still accesses thread 1's thread-local variables instead of its own. It's because an access to a thread-local variable does the following:
However, with optimization enabled, (1) and maybe (2) are being optimized away for the second access, since the compiler assumes a function that begins running on a particular thread will stay on that thread. This assumption is not true for my code.
How can I get the compiler to look at the correct thread-local storage both before and after the call to move_to_thread, without doing away with optimizations completely?
What happens if you try declaring your variable as follows:
__thread int volatile x = 0;
That should prevent the compiler from caching the value (though I'm not sure how volatile interacts with __thread).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With