iOS 多线程访问同一个属性,加锁防止出现意外修改的问题:
//上锁 和 解锁 宏定义 (使用信号量实现)
define Lock() dispatch_semaphore_wait(self->_semaphore, DISPATCH_TIME_FOREVER)
define Unlock() dispatch_semaphore_signal(self->_semaphore)
//成员变量 信号
dispatch_semaphore_t _semaphore;
Manager *manager;
(BOOL)connected {
Lock();
BOOL isConnect = manager.connectEnabled;
Unlock();
return isConnect;
}(void)setConnected:(BOOL) isConnect {
Lock();
manager.connectEnabled = isConnect;
Unlock();
}