Symbian
Symbian OS Library

FAQ-1089 Process local storage : How can I use TLS to implement writable global data which is shared between threads in the same process?

[Index][spacer] [Previous] [Next]



 

Classification: C++ Category: Development
Created: 07/15/2004 Modified: 03/01/2005
Number: FAQ-1089
Platform: ER5, Symbian OS v6.0, Symbian OS v6.1, Symbian OS v7.0, Symbian OS v7.0s, Symbian OS v8.0, Symbian OS v8.0a

Question:
TLS(thread local storage) allows you to associate a pointer to some global data G which will be returned each time Dll::Tls() is called.
The pointer returned is stored on a per dll, and per thread basis.
If your Dll code runs in main thread A and calls SetTls(G), then future calls to Tls() will return a pointer to G.
If another worker thread is spawned, and calls Tls() - it will return NULL since no data has been associated with this thread.


Answer:
To make G a unique instance for all threads created by the DLL, a scheme such as the following can be used:
  • Master thread creates a global object pointer G, and calls SetTls(G):
  • Each time the master thread creates a worker thread, it passes G into the thread creation function.
  • At the start of the worker thread function, the worker calls SetTls(G);

  • Each time the global data needs to be accessed from the DLL, Tls()->G->iThing is referenced. Perhaps with a macro. GLOBAL->iThing.