Re: Endless loop but only with release build
Re: Endless loop but only with release build
- Subject: Re: Endless loop but only with release build
- From: Chris Hanson <email@hidden>
- Date: Sat, 30 Sep 2006 21:35:47 -0700
On Sep 30, 2006, at 11:35 AM, Antonio Nunes wrote:
In practice, the loop should end almost immediately, because the
task in the other thread will check a variable that will cause it to
exit, so the busy-wait cycle is normally extremely short-lived. This
was just my rather simplistic way of making sure the secondary
thread exits before the window closes. But you're probably right
that compiler optimization is causing the release build to fail.
Regardless, you need to protect your use that shared variable with a
lock or other synchronization primitive. Doing so will ensure the
compiler doesn't optimize it out or keep its value solely in a
register (which means it won't be shared between the two threads), and
is also simply correct multithreaded programming practice.
Ultimately, if you're going to build a multithreaded application, you
should read up on the producer-consumer pattern and how to use it to
implement work queues. Following established and well-understood
patterns in writing multithreaded code will make it a lot easier to
ensure your code both performs well and avoids deadlocks or other
serious and difficult-to-correct threading bugs. It's also a lot
easier than winging it...
-- Chris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden