Threads

This is a discussion on Threads within the C Programming forums, part of the General Programming Boards category; Is it possible to have a variable visible only to a thread but not to other threads? ssharish...

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682

    Threads

    Is it possible to have a variable visible only to a thread but not to other threads?

    ssharish

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    Quote Originally Posted by ssharish2005 View Post
    Is it possible to have a variable visible only to a thread but not to other threads?

    ssharish
    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.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 12:28 PM
  2. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 01:43 AM
  3. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 06:27 AM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 03:39 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 04:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21