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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MFC static variable

MFC static variable

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorial
9 Posts 6 Posters 10 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.
  • B Offline
    B Offline
    bhanu_8509
    wrote on last edited by
    #1

    Dear All, I have a project with so many static variables accessed from one class to another class. I access these static variables by threads. For example, I access one variable in one thread to change the value and another thread to access it. Both threads are running concurrently. But sometime abruptly the static variable value got changed and it holds a very big value which results in some problem. I know that I use more static variables and I do mistake in programming but can anyone tell me avoid this kind of problem and why the variable is suddenly holding huge values. Many thanks in advance. Kind regards, Bhanu

    E I N R 4 Replies Last reply
    0
    • B bhanu_8509

      Dear All, I have a project with so many static variables accessed from one class to another class. I access these static variables by threads. For example, I access one variable in one thread to change the value and another thread to access it. Both threads are running concurrently. But sometime abruptly the static variable value got changed and it holds a very big value which results in some problem. I know that I use more static variables and I do mistake in programming but can anyone tell me avoid this kind of problem and why the variable is suddenly holding huge values. Many thanks in advance. Kind regards, Bhanu

      E Offline
      E Offline
      Eytukan
      wrote on last edited by
      #2

      Are you sure all those variables are properly synchronized for concurrent accesses?

      Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.

      B 1 Reply Last reply
      0
      • E Eytukan

        Are you sure all those variables are properly synchronized for concurrent accesses?

        Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.

        B Offline
        B Offline
        bhanu_8509
        wrote on last edited by
        #3

        Sorry I don't use any synchronization for the variables. But one thread is reading and another thread is writing. Please help me how to synchronize the variables. Many thanks for your reply.

        1 Reply Last reply
        0
        • B bhanu_8509

          Dear All, I have a project with so many static variables accessed from one class to another class. I access these static variables by threads. For example, I access one variable in one thread to change the value and another thread to access it. Both threads are running concurrently. But sometime abruptly the static variable value got changed and it holds a very big value which results in some problem. I know that I use more static variables and I do mistake in programming but can anyone tell me avoid this kind of problem and why the variable is suddenly holding huge values. Many thanks in advance. Kind regards, Bhanu

          I Offline
          I Offline
          Iain Clarke Warrior Programmer
          wrote on last edited by
          #4

          It sounds like you read the variable halfway through being changed. So, the variable is not a trivial type (ie, BOOL). Look at CCriticalSection, it's probably what you need. There's a million and one way to do this though. You could wrap the variable in a class including the CCriticalSection, and write get / set routines that use the critsect. It really depends on the fine details. If you have loads of static variables, it makes me suspect your initial design though. May be worth a second look. Does PlaceA need to know details of PlaceB? Iain.

          In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job! http://cv.imcsoft.co.uk/[^]

          B 1 Reply Last reply
          0
          • I Iain Clarke Warrior Programmer

            It sounds like you read the variable halfway through being changed. So, the variable is not a trivial type (ie, BOOL). Look at CCriticalSection, it's probably what you need. There's a million and one way to do this though. You could wrap the variable in a class including the CCriticalSection, and write get / set routines that use the critsect. It really depends on the fine details. If you have loads of static variables, it makes me suspect your initial design though. May be worth a second look. Does PlaceA need to know details of PlaceB? Iain.

            In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job! http://cv.imcsoft.co.uk/[^]

            B Offline
            B Offline
            bhanu_8509
            wrote on last edited by
            #5

            Iain Clarke wrote:

            If you have loads of static variables, it makes me suspect your initial design though. May be worth a second look. Does PlaceA need to know details of PlaceB?

            Yes PlaceA need to know the details of PlaceB. Can you please suggest me a quick way to overcome this problem.

            I 1 Reply Last reply
            0
            • B bhanu_8509

              Dear All, I have a project with so many static variables accessed from one class to another class. I access these static variables by threads. For example, I access one variable in one thread to change the value and another thread to access it. Both threads are running concurrently. But sometime abruptly the static variable value got changed and it holds a very big value which results in some problem. I know that I use more static variables and I do mistake in programming but can anyone tell me avoid this kind of problem and why the variable is suddenly holding huge values. Many thanks in advance. Kind regards, Bhanu

              N Offline
              N Offline
              Nemanja Trifunovic
              wrote on last edited by
              #6

              bhanu_8509 wrote:

              I have a project with so many static variables accessed from one class to another class. I access these static variables by threads.

              That's your problem. Is it possible in your case to send messages between threads, rather than accessing static variables directly?

              Programming Blog utf8-cpp

              1 Reply Last reply
              0
              • B bhanu_8509

                Iain Clarke wrote:

                If you have loads of static variables, it makes me suspect your initial design though. May be worth a second look. Does PlaceA need to know details of PlaceB?

                Yes PlaceA need to know the details of PlaceB. Can you please suggest me a quick way to overcome this problem.

                I Offline
                I Offline
                Iain Clarke Warrior Programmer
                wrote on last edited by
                #7

                bhanu_8509 wrote:

                Can you please suggest me a quick way to overcome this problem.

                No, only the slow way of proper design, and wrapping access to complex variables in critical sections. Iain.

                In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job! http://cv.imcsoft.co.uk/[^]

                CPalliniC 1 Reply Last reply
                0
                • I Iain Clarke Warrior Programmer

                  bhanu_8509 wrote:

                  Can you please suggest me a quick way to overcome this problem.

                  No, only the slow way of proper design, and wrapping access to complex variables in critical sections. Iain.

                  In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job! http://cv.imcsoft.co.uk/[^]

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

                  Iain Clarke wrote:

                  No, only the slow way of proper design, and wrapping access to complex variables in critical sections.

                  Well said, Euclid, with his "there is no royal road to geometry" is a newbie. :)

                  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?

                  1 Reply Last reply
                  0
                  • B bhanu_8509

                    Dear All, I have a project with so many static variables accessed from one class to another class. I access these static variables by threads. For example, I access one variable in one thread to change the value and another thread to access it. Both threads are running concurrently. But sometime abruptly the static variable value got changed and it holds a very big value which results in some problem. I know that I use more static variables and I do mistake in programming but can anyone tell me avoid this kind of problem and why the variable is suddenly holding huge values. Many thanks in advance. Kind regards, Bhanu

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #9

                    If the variables are not volatile, you're already in trouble. The ideal way would be to synchronize access to the variable using thread synchronization techniques (a critical section would do, as all your threads belong to the same process). I see you have a lot of things to read up.

                    It is a crappy thing, but it's life -^ Carlo Pallini

                    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