Before coroutines we used callbacks to run asynchronous operations. Callbacks was normal functions and could have thread_local
variables.
Let see this example:
void StartTcpConnection(void)
{
using namespace std;
thread_local int my_thread_local = 1;
cout << "my_thread_local = " << my_thread_local << endl;
auto tcp_connection = tcp_connect("127.0.0.1", 8080);
tcp_connection.async_wait(TcpConnected);
}
void TcpConnected(void)
{
using namespace std;
thread_local int my_thread_local = 2;
cout << "my_thread_local = " << my_thread_local << endl;
}
As we see from code, I have some (undocumented here) tcp_connect
function that connects to TCP endpoint and returns tcp_connection
object. This object can wait until TCP connection will really occur and call TcpConnected
function. Because we don't know specific implementation of tcp_connect
and tcp_connection
, we don't know will it call TcpConnected
on the same or on different thread, both implementations are possible. But we know for sure that my_thread_local
is different for 2 different functions, because each function has its own scope.
If we need this variable to be the same (as soon as thread is the same), we can create 3rd function that will return reference to thread_local
variable:
int& GetThreadLocalInt(void)
{
thread_local int my_variable = 1;
return my_variable;
}
So, we have full control and predictability: we know for sure that variables will be different if TcpConnected
and StartTcpConnection
will run on different threads, and we know that we can have them different or the same depending on our choice when these functions will run on the same thread.
Now let see coroutine version of the same operation:
void Tcp(void)
{
thread_local int my_thread_local = 1;
auto tcp_connection = co_await tcp_connect("127.0.0.1", 8080);
cout << "my_thread_local = " << my_thread_local << endl;
}
This situation is a bit questionable for me. I still need thread local storage, it is important language feature that I don't want to abandon. However, we here have 2 cases:
- Thread before
co_await
is the same one as afterco_await
. What will happen withmy_thread_local
? Will it be the same variable before and afterco_await
, especially if we'll useGetThreadLocalInt
function to get its reference instead of value? - Thread changes after
co_await
. Will C++ runtime reinitializemy_thread_local
to value from new thread, or make a copy of previous thread value, or may be use reference to the same data? And similar question forGetThreadLocalInt
function, it returns reference tothread_local
object, but the reference storage itself isauto
, will coroutine reinitialize it to new thread, or we'll get (dangerous!!!) race condition, because thread 2 will strangely get reference to thread 1 thread local data and potentially use it in parallel?
Even it is easy to debug and test what will happen on any specific compiler, the important question is whether standard says us something about that, otherwise even if we'll test it on VC++ or gcc an see that it behaves somehow on these 2 popular compilers, the code may loose portability and compile differently on some exotic compilers.
co_await
, a different thread opens the coroutine, it will access its instance of thread local variable.thread_local
variable in C++ the compiler emits code that calls OS level API to access thread local storage instead of doing it one time and using previously received pointer to thread local data? I. e. that codethread_local o = new object(); o.method1();o.method2();o.method3()
will emit code that callsTlsGetValue
4 times? Is it guarantee of standard? Or any optimization may occur that insignificant for standard function but may change behavior of coroutine?co_await
, so it knows that afterwards, it needs to access the memory again instead of using a cached copy. This is no different from accessing a global and calling a function the compiler can't see the definition of; the compiler must assume that the global was changed by the function call, so later accesses to the function have to be real memory fetches.