Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

McBgnr

macrumors regular
Original poster
Apr 13, 2009
144
0
Hi,

I am NSThread in my application for some background task. I have a button thru which the thread can be cancelled in 10.5. However, 10.4 does not have cancel method, so I was wondering how to implement the functionality to stop the thread midway for it. Any ideas?
 
Have a BOOL protected by a lock (or, equivalently, an atomic BOOL property) that you check periodically from the background thread and set from the foreground thread when you need to cancel.
 
Remember that you can't just say "thread go away" and have that happen safely. While POSIX allows for asynchronous cancels, it's regarded as a bad idea. The thread needs to clean up resources it's using in order to avoid memory leaks and certain forms of contention. Catfish has the best advice for you in this case, imho.
 
Hmm... you'd need to declare it volatile if you want to not use a lock. Otherwise the compiler is free to hoist the check out of the loop. i.e. convert this:

Code:
while(!cancelled) {}

into

Code:
if (!cancelled) {
    while(1) {}
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.