views:

205

answers:

6

I am using ACE threads and need each thread to have its own int member. Is that possible?

+2  A: 

ACE calls this "Thread Specific Storage". Check this out: ACE_TSS. That's about all I know about it, sorry can't be more help.

The Wikipedia page for thread-local storage says there is a pthreads way to do this too.

Dan
It's sometimes calls TSS or TSD (thread-specific data) in pthreads. Here's a link that describes how to create such data using the pthreads API: http://www.opengroup.org/onlinepubs/000095399/functions/pthread_key_create.html. On UNIX/UNIX-like platforms, ACE just uses pthreads under-the-hood.
Void
A: 

Its platform specific. Windows for instance you should use __declspec( thread ). The compiler will leverage the TLS API (TlsAlloc, TlsFree and friends), and on Win32 you shouldn always use FLS (Fiber Local Storage) instead of TLS, but the TLS API is silently redirecting you to FLS anyway on any modern Win32 version.

Remus Rusanu
A: 

Yes. You can use the ACS_TSS<type> template, which is designed for "thread specific storage" (ie: thread local variables).

For details, see the docs on ACE_TSS.

Reed Copsey
do you know how i can set initial value for all threads?
amitlicht
A: 

GCC directly supports TLS for some targets. You can use the GCC-specific __thread keyword for defining thread-local variables (must be static or global).

libACE itself has thread-local stuff built in, you can check out the documentation and look at the example code.

AndiDog
A: 

Thread Local Storage at MSDN site is great resource Thread Local Storage template, sample template for making Thread Local Storage easier to use in C++.

lsalamon
A: 

There's no way to have ACE_TSS set the initial value for all threads; you can easily set the initial value just after entry in your thread function though.

Steve Huston