Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2005

Tip: Looking for answers? Try searching our database.

debugging multithreaded app

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bruno van Dooren - 16 Mar 2005 20:15 GMT
Hi,

i was debugging a multithreaded app, when i stumbled across some weird
behavior of the debugger.

in the destructor of my main object i send a stop event to the worker
thread, and then wait until that thread has finished.

m_Stop = true;
while(GetExitCodeThread() == STILL_RUNNING)
   Sleep(50);

the strange thing is that if i break just before the m_Stop = true, and then
step through the while loop, the exit code is STILL_RUNNING forever. i can
step forever without the thread ever ending.
if a break just after the while loop, the thread has stopped without a
problem.

is this normal behavior?

kind regards,
   Bruno.
William DePalo [MVP VC++] - 16 Mar 2005 21:08 GMT
> i was debugging a multithreaded app, when i stumbled across some weird
> behavior of the debugger.
[quoted text clipped - 13 lines]
>
> is this normal behavior?

<guess mode on>
Could it be related to the fact that the debugger suspends _all_ threads on
encountering the breakpoint? Not having seen the source, I don't know how it
resumes them after the breakpoint is hit, but if it does it in a "lazy" way
in which the debugged thread is resumed first while the background threads
are resumed after the fact and if that resumption takes more than 50ms then
it may be that the other thread never runs.
</guess mode on>

What does the other thread do? Is it guaranteed to stop after you set
m_stop? If so, why use the hokey (IMO, YMMV) busy loop?

Regards,
Will
Bruno van Dooren - 18 Mar 2005 09:14 GMT
>> i was debugging a multithreaded app, when i stumbled across some weird
>> behavior of the debugger.
[quoted text clipped - 28 lines]
> Regards,
> Will

The reason for the while loop is that after the threads are stopped, a lot
of shared resources like notifiers and shared memory are cleaned up.
if i should not wait for the threads to finish, there is a race condition
between the functionality in the threads, and the cleanup code.

ps i just noticed that my code looked like
while(..)
   ;

without sleep. as a result there was probably not enough time for the
threads to resume.
still, i would have thought that it would only break the thread in which the
breakpoint was encountered.

thanks.
   Bruno.
Tim Robinson - 16 Mar 2005 22:38 GMT
> i was debugging a multithreaded app, when i stumbled across some weird
> behavior of the debugger.
[quoted text clipped - 5 lines]
> while(GetExitCodeThread() == STILL_RUNNING)
>    Sleep(50);
[...]

I can't claim any insight into your problem, but this is the traditional way
to wait for a thread to exit:

m_Stop = true;
WaitForSingleObject(m_ThreadHandle, INFINITE);

There's no need to poll, as the original code does.

Signature

Tim Robinson
MVP, Windows SDK

Bruno van Dooren - 18 Mar 2005 09:15 GMT
>> i was debugging a multithreaded app, when i stumbled across some weird
>> behavior of the debugger.
[quoted text clipped - 14 lines]
>
> There's no need to poll, as the original code does.

   Thanks. i didn't know.
       Bruno.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.