I am using ACE threads and need each thread to have its own int member. Is that possible?
-
Out of curiosity, do you really need to use TSS? Can you get away with simply declaring variables on your thread's stack, or are you looking for some way to prevent other threads from accessing your variable? If the latter, why not use locks? Some folks feel that TSS is mostly a crutch for legacy interfaces (e.g. making errno thread safe), and that locks are the better way to go. I'm not advocating one or the other. YMMV. – Void - Othman Mar 10 '10 at 22:11
-
I think that TSS is actually the most elegant solution in this case. I have n threads, each should have its own variable (with its own value). – amitlicht Mar 10 '10 at 22:44
-
2You mention wanting each thread to have its own "member." That suggests you're using classes to represent your threads. If so, then simply give the class a normal member variable. Each thread has its own instance of the class, so it also has its own separate member variable. – Rob Kennedy Mar 11 '10 at 01:06
5 Answers
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.

- 5,929
- 6
- 42
- 52
-
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 - Othman Mar 10 '10 at 21:44
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.

- 288,378
- 40
- 442
- 569
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.

- 554,122
- 78
- 1,158
- 1,373
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.

- 68,631
- 21
- 159
- 205
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.

- 1,432
- 13
- 20