Thread: Is getting object from Thread local storage expensive?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Is getting object from Thread local storage expensive?

    Is there a big performance hit in frequently retrieving an object from thread local storage? (Assume that it's stored once per thread and never changed but is retrieved many times)

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Define "big", "frequently", and "many". If you have an existing program about which you are wondering, profile it and see if there is indeed a penalty.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What architecture is this on?

    Most x86 OS's use FS or GS segment register as a pointer to the TLS base, so the access to get the TLS pointer will require an extra attribute, but it shouldn't make much difference compared to using a pointer to get the data in the first place. I doubt you could measure the difference here.

    If the TLS base is not in a segment register, then it may require a bit more "effort" from the processor to dig out the correct data.

    For example, in some systems, it involves a "GetThreadId()" system call, and then an index into a table - that could easily take a few hundred or more clock-cycles.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 01:08 AM
  2. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  3. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 04:33 AM
  4. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  5. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 10:27 PM