iOS 多線程訪問同一個屬性,加鎖防止出現(xiàn)意外修改的問題:
//上鎖 和 解鎖 宏定義 (使用信號量實現(xiàn))
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();
}