| |||
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] |
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.)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...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.
| 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>) |
| Home | Archives | FAQ | Terms/Conditions | Contact | RSS | Lists | About |
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.