Is it possible to have a variable visible only to a thread but not to other threads?
ssharish
Is it possible to have a variable visible only to a thread but not to other threads?
ssharish
Short answer: no, not really.
Long asnwer: There are ways to make this happen, but there's no DIRECT way to do that.
There are such things as "TLS" - Thread Local Storage (not to be confused with the secure network protocol by the same name) which allows each thread to fetch a pointer to "its data". Most OS's with thread-support will support this concept.
Another way to achieve something similar is to store data in an array that is indexed on some hash of the thread-id [or each thread given a unique number when they first request it, and then use that as a handle to fetch data with].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Define "visible." All memory maps are shared between threads, so if a piece of data in present at a specific address in one thread, it will be at that same address in any other thread. In that sense all variables are always visible to all threads.
But if you are talking about scope, then local variables are all basically "invisible" to other threads since they can't be accessed by name. Of course, you could pass a pointer to a local variable to some other thread, and it could access it through that pointer, but that's only possible if you do it on purpose.
(And passing pointers to local variables to other threads has all the same problems as returning a pointer to a local variable -- when the function terminates, the variable goes out of scope and pointers dangle.)