开发者

One thread changes BOOL to YES, another thread doesn't see change

开发者 https://www.devze.com 2023-01-17 00:22 出处:网络
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

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?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消