Click here to Skip to main content
15,904,934 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Problem Fixed -- optimization (/O2) causes these problems Pin
Cyrilix13-Jun-07 7:02
Cyrilix13-Jun-07 7:02 
Questionutyutyuytuutyutyutyuutyutyutyutyutyutyu/,.//,./, Pin
suchon_phuong13-Jun-07 5:33
suchon_phuong13-Jun-07 5:33 
AnswerRe: utyutyuytuutyutyutyuutyutyutyutyutyutyu/,.//,./, Pin
Cedric Moonen13-Jun-07 7:11
Cedric Moonen13-Jun-07 7:11 
QuestionDraw a cube Pin
gentleguy13-Jun-07 5:18
gentleguy13-Jun-07 5:18 
AnswerRe: Draw a cube Pin
Dustin Henry13-Jun-07 6:51
Dustin Henry13-Jun-07 6:51 
QuestionOnMouseMove HELP Please Pin
Charlie Curtis13-Jun-07 3:39
Charlie Curtis13-Jun-07 3:39 
AnswerRe: OnMouseMove HELP Please [modified] Pin
Mark Salsbery13-Jun-07 7:19
Mark Salsbery13-Jun-07 7:19 
QuestionC++ Thread Memory States/References Pin
Eshazear13-Jun-07 3:36
Eshazear13-Jun-07 3:36 
Hey,

I've been busy with C++ for many years now, and this particular problem has always been eluding me.
I'm using Visual C++ 6.0, with Service packs 5&6 installed, as well as processor pack 5.
My machine is running Windows XP SP2, and hyperthreading is enabled.


To Illustrate my problem I've created a simple test app. :
In a multi-threaded C++ Application, I have a Parent Dialog, which creates(runs) 2 independent threads.

I have assigned each of the 2 threads, intensive, continuous computational work within a while loop. (The loop criteria is met and breaks when the parent sets a member value requesting them to exit)

This Test : starting and running both threads simultaneously and then shutting them down again, works perfectly, in debug.
However, once I compile and run the project in release, neither of the threads seem to register that the the loop criteria variable has changed.

------------------------------------------------------
Thread Code :
------------------------------------------------------
CTestThread1Dlg* parent = (CTestThread1Dlg*) param;
parent->m_Thread1Running = true;
parent->m_Thread1Starting = false;

while (parent->m_RunThread1)
{
parent->m_Th1Value++;
}

parent->m_Thread1Running = false;
_endthread();
------------------------------------------------------

But this only happens when the thread is using all the computational power that it can get (no interruptions, ie. conditionals, Sleeps, etc).
If I were to introduce a "Sleep(15)" right after the increment, then the thread would immediately register a change in "m_RunThread1" and exit, the moment the parent changes "m_RunThread1".

With the given code above(Thread Code), in release, the thread does not register and break the while loop when I call code from the parent, such as :
------------------------------------------------------
Parent Code :
------------------------------------------------------
m_RunThread1 = false;
Beep(450, 50);
Beep(450, 50);
while (m_Thread1Running || m_Thread1Starting)
Sleep(100);
AfxMessageBox("Thread1 has exited");
------------------------------------------------------

I added the 2 beeps so that I also have audio confirmation that "m_RunThread1", was indeed set - and I hear the beeps, but the threads keep going.

Do the threads have a type of duplicate memory state of all the members that they access, (which are only updated once the thread's CPU time has elapsed, and it is removed from the stack for the next thread)?

That does not make logical sense to me, but it is the only reason that I could think of, explaining why the parents' "parent->m_RunThread1" is not 'updated' until the thread's execution cycle is interrupted...

Has anyone else experienced this behavior?
I've currently pinned down the problem within a huge multi-threaded application, and unless I can identify how to solve it in this simple test case, there is no hope of fixing it :/

Any info. would be appreciated, thanks.

AnswerRe: C++ Thread Memory States/References Pin
James R. Twine13-Jun-07 3:46
James R. Twine13-Jun-07 3:46 
GeneralRe: C++ Thread Memory States/References Pin
Eshazear13-Jun-07 4:13
Eshazear13-Jun-07 4:13 
GeneralRe: C++ Thread Memory States/References Pin
Roger Stoltz13-Jun-07 4:35
Roger Stoltz13-Jun-07 4:35 
GeneralMultithreaded aproaches Pin
Eshazear13-Jun-07 4:45
Eshazear13-Jun-07 4:45 
QuestionVolatile Array allocation on the heap? Pin
Eshazear14-Jun-07 1:33
Eshazear14-Jun-07 1:33 
QuestionNeed Help.. Pin
rajeevktripathi13-Jun-07 3:12
rajeevktripathi13-Jun-07 3:12 
QuestionRe: Need Help.. Pin
Hamid_RT13-Jun-07 3:20
Hamid_RT13-Jun-07 3:20 
AnswerRe: Need Help.. Pin
Hamid_RT13-Jun-07 3:24
Hamid_RT13-Jun-07 3:24 
GeneralRe: Need Help.. Pin
rajeevktripathi13-Jun-07 19:23
rajeevktripathi13-Jun-07 19:23 
QuestionRe: Need Help.. Pin
Hamid_RT13-Jun-07 20:57
Hamid_RT13-Jun-07 20:57 
GeneralRe: Need Help.. Pin
rajeevktripathi14-Jun-07 1:26
rajeevktripathi14-Jun-07 1:26 
QuestionNeed Help.... Pin
rajeevktripathi13-Jun-07 3:11
rajeevktripathi13-Jun-07 3:11 
AnswerRe: Need Help.... Pin
James R. Twine13-Jun-07 3:25
James R. Twine13-Jun-07 3:25 
GeneralRe: Need Help.... Pin
rajeevktripathi13-Jun-07 19:21
rajeevktripathi13-Jun-07 19:21 
AnswerRe: Need Help.... Pin
Programm3r13-Jun-07 4:18
Programm3r13-Jun-07 4:18 
QuestionShowing/hiding Groups of controls Pin
softwaremonkey13-Jun-07 2:47
softwaremonkey13-Jun-07 2:47 
AnswerRe: Showing/hiding Groups of controls Pin
Hamid_RT13-Jun-07 3:17
Hamid_RT13-Jun-07 3:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.