Global Variable..(Simple but difficult....)
-
Hi.. I'm using Global variable in VC++ MFC by declaring variable in .cpp file. then I've to use "extern" to acces that variable from other file..that's OK!..if I'm working normal..But when I use to access that Variable from thread..then It gives Error in Relase varsion(NOTE: not giving error in DEBUG version).. 1. Whenever I need to write new value I use CCriticalSection variable & Lock & unlock while operation of writing.. e.g: CCriticalSection a1; a1.Lock(); bStopRightDownThread=true; a1.Unlock(); 2. This value is Continue used by thread & checked by thread..if this value change then thread take action as per value... Problem : It is giving Illigal Operation error & close application in case of RELEASE version, but working fine in case of DEBUG version. WHAT DO YOU THINK REASON OF THIS? THanks..I'm waiting for your reply...:) ---Sumit Kapoor--- sumit_kapoor1980@hotmail.com
-
Hi.. I'm using Global variable in VC++ MFC by declaring variable in .cpp file. then I've to use "extern" to acces that variable from other file..that's OK!..if I'm working normal..But when I use to access that Variable from thread..then It gives Error in Relase varsion(NOTE: not giving error in DEBUG version).. 1. Whenever I need to write new value I use CCriticalSection variable & Lock & unlock while operation of writing.. e.g: CCriticalSection a1; a1.Lock(); bStopRightDownThread=true; a1.Unlock(); 2. This value is Continue used by thread & checked by thread..if this value change then thread take action as per value... Problem : It is giving Illigal Operation error & close application in case of RELEASE version, but working fine in case of DEBUG version. WHAT DO YOU THINK REASON OF THIS? THanks..I'm waiting for your reply...:) ---Sumit Kapoor--- sumit_kapoor1980@hotmail.com
You can try this: From thread post/send message which calls a function from your class which will set your variable. I once had something similar and we thought the reason was that a thread works on a different part of memory. (but I'm not sure) Hope this helps. "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
-
Hi.. I'm using Global variable in VC++ MFC by declaring variable in .cpp file. then I've to use "extern" to acces that variable from other file..that's OK!..if I'm working normal..But when I use to access that Variable from thread..then It gives Error in Relase varsion(NOTE: not giving error in DEBUG version).. 1. Whenever I need to write new value I use CCriticalSection variable & Lock & unlock while operation of writing.. e.g: CCriticalSection a1; a1.Lock(); bStopRightDownThread=true; a1.Unlock(); 2. This value is Continue used by thread & checked by thread..if this value change then thread take action as per value... Problem : It is giving Illigal Operation error & close application in case of RELEASE version, but working fine in case of DEBUG version. WHAT DO YOU THINK REASON OF THIS? THanks..I'm waiting for your reply...:) ---Sumit Kapoor--- sumit_kapoor1980@hotmail.com
Your code for using a critical section is flawed. What you have there won't do anything. The critical section object must be shared just like the boolean you're trying to modify:
CCriticalSection _bStopRightDownThread;
bool bStopRightDownThread;
...
_bStopRightDownThread.Lock();
bStopRightDownThread = true;
_bStopRightDownThread.Unlock();The fact that it produces an error in the release version but not in the debug version is (in my experience) totally coincidental.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Your code for using a critical section is flawed. What you have there won't do anything. The critical section object must be shared just like the boolean you're trying to modify:
CCriticalSection _bStopRightDownThread;
bool bStopRightDownThread;
...
_bStopRightDownThread.Lock();
bStopRightDownThread = true;
_bStopRightDownThread.Unlock();The fact that it produces an error in the release version but not in the debug version is (in my experience) totally coincidental.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Ya..Dear, Ya Dear your are right, I'm facing problem in release version,not in Debug. But can you give more clear idea..how to do? // Take these as Global CCriticalSection _bStopRightDownThread; bool bStopRightDownThread; ..... ..... ..... // In particular function use as follow _bStopRightDownThread.Lock(); bStopRightDownThread = true; _bStopRightDownThread.Unlock(); Do Your means this? If yes..then I'm using this in many files, DO u means I've to share Critical section vaiable to all using: extern CCriticalSection _bStopRightDownThread; or induvaully declare in all files.. PLz Gide a One step more.. Thanks for reply.. ---Sumit Kapoor--- sumit_kapoor1980@hotmail.com
-
Ya..Dear, Ya Dear your are right, I'm facing problem in release version,not in Debug. But can you give more clear idea..how to do? // Take these as Global CCriticalSection _bStopRightDownThread; bool bStopRightDownThread; ..... ..... ..... // In particular function use as follow _bStopRightDownThread.Lock(); bStopRightDownThread = true; _bStopRightDownThread.Unlock(); Do Your means this? If yes..then I'm using this in many files, DO u means I've to share Critical section vaiable to all using: extern CCriticalSection _bStopRightDownThread; or induvaully declare in all files.. PLz Gide a One step more.. Thanks for reply.. ---Sumit Kapoor--- sumit_kapoor1980@hotmail.com
If you are sharing a global variable, then you must share the critical section as well. Each function must lock the same critical section, otherwise they don't do anything. So you'll have to define it as
extern
if it's used in multiple files.Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"