Thread: dark side of global variables

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by matsp
    That does make the variable a non-global, as it's now a Thread-Local-Storage (TLS) variable - and I much prefer to not use compiler-specific extension to make TLS work - as most OS's and compilers support TLS, but no other compiler than MS supports this keyword (hence the __ to indicate that it's non-standard).
    While TLS is not standard, it is certainly an option that defies the problems posed by multi-threading without simply hoping volatile will save the day. I never said I was pro-globals. I am simply offering solutions posed by the original poster's scenario.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Matus View Post
    While TLS is not standard, it is certainly an option that defies the problems posed by multi-threading without simply hoping volatile will save the day. I never said I was pro-globals. I am simply offering solutions posed by the original poster's scenario.
    Yes, but it's no longer a global variable - it's a variable accessed through a pointer (in some way - it may be an offset from a segment variable in x86, but that's for all intents and purposes a pointer anyways) - and you can always do that yourself with a little bit of extra pointer stuff.

    And volatile has nothing at all to do with TLS - it removes the need to use volatile because the memory is no longer accessible from multiple threads, but it's otherwise unrelated to volatile. Volatile simply means "compiler, please be aware that the use of this variable may change even if you don't think it will from looking at the code", which essentially means "do not optimize away reads/writes of the variable", and it was originally introduced in C as a means to allow the compiler to optimize your normal variables, but tell the compiler that hardware registers that aren't memory needs to be treated differently. It may come in handy with threads too, but it's a side-effect.

    --
    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
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by matsp View Post
    --
    Mats

    Kk Mats no problemo, good reading, good posts!
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just a point on "reading through the code": I did "dir /s *.h *.cpp" on ONE of the directories of the code I'm working on, that I have worked a bit with: 2.9MB. Another one, that I know very little of: 11.3MB.

    Entire tree of our groups work: 35 MB (in about 3000 files).

    That's an awful lot of code to "read through" - in fact, I couldn't do my job if I read through all the code before I worked on it - even in a small project with, say, a few hundred kilobytes of code, to understand the use of all variables in all places becomes an impossible task.

    --
    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. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. Use global variables or pointers?
    By RealityFusion in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2005, 08:47 PM
  4. Global variables.. how bad?
    By punkrockguy318 in forum C++ Programming
    Replies: 19
    Last Post: 11-30-2003, 10:53 PM
  5. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10:28 AM

Tags for this Thread