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: John Stiles <email@hidden>
- Date: Sat, 30 Sep 2006 22:16:55 -0700
Chris Hanson wrote:
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.
The "volatile" keyword on the variable may be enough in this case. (I
don't remember the OP's question; I'm just reading between the lines.)
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...
I agree. Multithreaded programming is extremely challenging, doubly so
when you don't know all of the issues. Following preexisting convention
will give you a solid foundation to work on.
_______________________________________________
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