Re: [AD] NewAPI Poll

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Tuesday 21 February 2006 06:04, Elias Pschernig wrote:
> If it is a thread-local variable, it can't possibly be different. But
> yes, it's something to think about. If we can't use thread-local
> variables, but a real global, then changing the render target in other
> threads should probably be avoided.

C99 defines the storage specifier __thread for exactly this.
-
__thread AL_CONTEXT *active_context;
-
That will make active_context be different for every thread it's used in 
(though if you grab the pointer to it in one thread and pass that around to 
different threads, it'll modify the copy from the thread the pointer 
originated from).
http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Thread-Local.html
http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/C99-Thread-Local-Edits.html#C99%20Thread-Local%20Edits

You'll probably balk at this since it's C99 and not available in MSVC, but 
don't worry. MS has something similar, but obviously it has to be named 
differently and with possibly slightly different behavior:
-
__declspec(thread) AL_CONTEXT *active_context;
-
Unfortuantely, according to MSDN, this will cause problems (read: crashes) 
when using it in a DLL loaded with LoadLibrary (and as I understand it, 
that's one of the features being shot for; run-time loading via dlopen and 
LoadLibrary?). To get around this, MSDN gives an example of using DllMain to 
automatically create/allocate/free/destroy TLS objects:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/using_thread_local_storage_in_a_dynamic_link_library.asp

I am unsure if C99's __thread has the same short-comings of VC's 
__declspec(thread). The GCC pages linked above don't mention anything about 
shared libraries. If so, MinGW can use VC's DllMain hack. Unix, however, may 
be a little trickier. But if someone can show me a way to specify a function 
to run when the DLL is first loaded for a process, and finally freed, as well 
as when another thread starts and ends, I can devise a way for it to work. 
AFAIK, GCC has __attribute__((constructor)) and __attribute__((destructor)) 
which will fill the process beginning and end nicely.




Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/