wxThread, thread local storage and wxLog

Armel Asselin asselin.armel at wanadoo.fr
Tue Aug 8 11:39:13 PDT 2006


Hello all,

some times ago, i launched a discussion about thread local storage. there 
was no final decision about what should be done.
wxLog stuff currently has a problem: it is not easy at all to handle log 
messages coming from worker threads because:
- they are generated by wx code itself.
- they go through the active log target
- they got messed up inside this single log target
- disabling logging is also not thread safe

One solution for the wxLog problem would be to have one target per thread 
(stored in a TLS=Thread Local Storage).

As an example, imagine multiple internet clients threads, each handling a 
different web site: you want a log output per thread. In this case, only TLS 
can help.

The TLS can well be stored in the wxThread object (with potentially a 
special case for main thread, but that's no hard to solve).

I propose to add:
- static int wxThread::AllocateTlsEntry( int id ): id should be in a range 
defined by wx, the returned value would be an offset for that id, we could 
arrange to have id => offset mapping to be the same for all threads, it 
would allow having global variables being initialized once with this offset 
(avoiding frequent calls to AllocateTlsEntry)
- static void *wxThread::GetTlsEntry (int offset): returns the "offset" 
entry of the TLS (a simple indirection)
- static void wxThread::SetTlsEntry (int offset, void *value) : set the 
value (never deleted by tls)
static void wxThread::SetTlsEntry (int offset, wxObject *value, bool owned): 
set the value, deleted at thread destruction if <owned>

(these functions could be as well put into a wxTLS object in order not to 
pollute wxThread, but in these case the absence of a system based TLS could 
make it harder to port the object)

For wxLog:
- potentially: add: static void wxLog::SetActiveTargetPerThread( bool 
enable ): if !enable (default), active target is not in TLS but inside a 
singleton as before.
- wxLog static variables would be set in an object reachable through an 
entry in TLS

Regards
Armel Asselin
----------------------------------------------------
http://www.elliecomputing.com
ECMerge - comparison and merge tool





More information about the wx-dev mailing list