Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Access Global Variable.

Access Global Variable.

Scheduled Pinned Locked Moved C / C++ / MFC
questioncareer
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G gothic_coder

    I have been asked this question in my recent interview... "How can we make sure that only one thread access global variable?" What's the appropriate answer. Thanks all

    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #2

    Using "Synchronization". :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
    [My articles]

    In testa che avete, signor di Ceprano?

    L 1 Reply Last reply
    0
    • G gothic_coder

      I have been asked this question in my recent interview... "How can we make sure that only one thread access global variable?" What's the appropriate answer. Thanks all

      R Offline
      R Offline
      rp_suman
      wrote on last edited by
      #3

      Mutex Objects

      -- "Programming is an art that fights back!"

      T 1 Reply Last reply
      0
      • R rp_suman

        Mutex Objects

        -- "Programming is an art that fights back!"

        T Offline
        T Offline
        ThatsAlok
        wrote on last edited by
        #4

        rp_suman wrote:

        Mutex Objects

        Critical section at the time of accessing of the variable! not mutex if we talking about thread! mutex is more beneficial in case of limiting access across different process

        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
        Never mind - my own stupidity is the source of every "problem" - Mixture

        cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

        1 Reply Last reply
        0
        • G gothic_coder

          I have been asked this question in my recent interview... "How can we make sure that only one thread access global variable?" What's the appropriate answer. Thanks all

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #5

          The first response is to gently chide the interviewer telling him (or her) that they're mad for using global variables in a multi-threaded program. Once they've made some excuse for using global data (legacy code, have to make change immediately otherwise the four horsemen ride, Jormangand wakes or even worse the company's stock price falls) the second thing to do is turn the question around and ask whether they mean from one thread at a time or only from one thread? If they say from only one thread EVER then say you'd use thread local storage (TLS) or refactor the code to pass the parameter down the call stack from the function the thread started in. If they "from one thread at a time" say you'd use the appropriate type of lock - usually a type of mutex. The final thing before the interviewer moves on is to ask whether programmers use a lot of globals at this company... If they do consider asking for more money as you'll need it for the extra hours spent debugging gash code (and yes, this is from recent personal experience...) Cheers, Ash

          G 1 Reply Last reply
          0
          • A Aescleal

            The first response is to gently chide the interviewer telling him (or her) that they're mad for using global variables in a multi-threaded program. Once they've made some excuse for using global data (legacy code, have to make change immediately otherwise the four horsemen ride, Jormangand wakes or even worse the company's stock price falls) the second thing to do is turn the question around and ask whether they mean from one thread at a time or only from one thread? If they say from only one thread EVER then say you'd use thread local storage (TLS) or refactor the code to pass the parameter down the call stack from the function the thread started in. If they "from one thread at a time" say you'd use the appropriate type of lock - usually a type of mutex. The final thing before the interviewer moves on is to ask whether programmers use a lot of globals at this company... If they do consider asking for more money as you'll need it for the extra hours spent debugging gash code (and yes, this is from recent personal experience...) Cheers, Ash

            G Offline
            G Offline
            gothic_coder
            wrote on last edited by
            #6

            Thanks guys, Synchronization is what i replied, But as Aescleal get it right the interviewer was asking that the variable must be accessed only and only by one thread.. So i guess the TLS is the answer he wants, I need to see these thing in deep :) Nevertheless i got the job :)

            1 Reply Last reply
            0
            • CPalliniC CPallini

              Using "Synchronization". :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #7

              That does not limit the number of threads accessing a variable, it merely sequences the accesses, making sure they don't overlap. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

              CPalliniC 1 Reply Last reply
              0
              • L Luc Pattyn

                That does not limit the number of threads accessing a variable, it merely sequences the accesses, making sure they don't overlap. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #8

                Of course. It wasn't clear to me what the interviewer really asked to the OP. BTW you may use a synchronization object to prevent other threads accessing the resource, if hanging the other threads is an option... :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                modified on Thursday, June 17, 2010 8:40 AM

                In testa che avete, signor di Ceprano?

                L 1 Reply Last reply
                0
                • CPalliniC CPallini

                  Of course. It wasn't clear to me what the interviewer really asked to the OP. BTW you may use a synchronization object to prevent other threads accessing the resource, if hanging the other threads is an option... :-D

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  modified on Thursday, June 17, 2010 8:40 AM

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #9

                  We both read the same sentence, which is pretty clear IMO. I took it as is, you chose to read something that wasn't really there... Hanging is not an option, this ain't 19th century Texas. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                  CPalliniC 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    We both read the same sentence, which is pretty clear IMO. I took it as is, you chose to read something that wasn't really there... Hanging is not an option, this ain't 19th century Texas. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                    Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #10

                    Yes, indeed it was pretty clear and I really chose to read it that way (I've updated my previous reply). Hanging threads is an everyday option for many 'senior developers' around... :-D

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    In testa che avete, signor di Ceprano?

                    L 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      Yes, indeed it was pretty clear and I really chose to read it that way (I've updated my previous reply). Hanging threads is an everyday option for many 'senior developers' around... :-D

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #11

                      :thumbsup:

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                      Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups