Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Endless loop but only with release build





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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden


This email sent to email@hidden
References: 
 >Endless loop but only with release build (From: Antonio Nunes <email@hidden>)
 >Re: Endless loop but only with release build (From: Bill Bumgarner <email@hidden>)
 >Re: Endless loop but only with release build (From: Antonio Nunes <email@hidden>)
 >Re: Endless loop but only with release build (From: Chris Hanson <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.