Task: TLS On Demand
The perspective of C towards TLS is the following: everything is shared by default. This makes multithreading easier and more lightweight to implement than in other languages, like D, because synchronization is left entirely up to the developer, at the cost of potential unsafety.
Of course, we can specify that some data belongs to the TLS, by preceding the declaration of a variable with __thread keyword. Enter labs/lab-08/tasks/tls-on-demand/. Now enter support/src and follow the TODOs.
-
Create the declaration of
varand add the__threadkeyword to place the variable in the TLS of each thread. Recompile and run the code a few more times. You should see that in the end,varis 0. -
Print the address and value of
varin each thread. See that they differ. -
Modify the value of
varin themain()function before callingpthread_create(). Notice that the value doesn’t propagate to the other threads. This is because, upon creating a new thread, its TLS is initialised.