views:

123

answers:

3

I have a property declared as the following:

@property(assign) BOOL die;

One thread continuously checks if it should die by looking to see if that variable has changed to YES. When that die is set to YES (by a user clicking a button), the other thread that is grinding away still sees it as NO. I've put careful traces through the code and seen that the variable definitely doesn't show up as modified. What is going on here?

Does each thread contain its own cache of the variable? In Java, my native language, I would have set the 'volatile' keyword on it to remove local thread caching on the property.

Is this something you can do in obj-c or am I on the wrong track?

+1  A: 

different threads might be checking different instances. make sure that both threads are accessing to the same copy of that parameter

Zuuum
A: 

You might be better using using notification centers, and so that the listener is notified when it is changed.

MCannon
A: 
JonLOo
This won’t work across threads—notifications are posted to the thread they’re dispatched on. You could eat that notification and then post it to the other thread’s runloop but the OP’s code should work, modulo other bugs in the OP’s code.
Ben Stiglitz
what about NSDistributedNotificationCenter ??
JonLOo