MFC static variable
-
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
-
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
-
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.
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.
-
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
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/[^]
-
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/[^]
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.
-
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
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?
-
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.
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/[^]
-
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/[^]
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] -
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
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