Quantcast
Channel: User trojanfoe - Stack Overflow
Viewing all articles
Browse latest Browse all 226

Will this RAII-style Objective-C class work?

$
0
0

Under C++, I have a Mutex class, and I use this RAII-style class to ensure the mutex is unlocked, regardless of the reason for the method return:

class MutexLock {protected:    Mutex &m_mutex;public:    MutexLock(Mutex &mutex) :        m_mutex(mutex) {        m_mutex.lock();    }    ~MutexLock() {        m_mutex.unlock();    }};

Is there any reason, and when using ARC, that an equivalent Objective-C class wouldn't work just as well:

@interface Locker : NSObject {    NSLock *_lock;}- (void)setLock:(NSLock *)lock;@end@implementation Locker- (void)setLock:(NSLock *)lock {    [_lock unlock];    _lock = lock;    [_lock lock];}- (void)dealloc {    self.lock = nil;}@end

Which might be used in the following way:

NSLock *_lock;    // instance variable- (void)myFunc {    Locker *locker = [[Locker alloc] init];    locker.lock = _lock;    return;     // Whenever I like}

I understand it won't work in the case of Objective-C exceptions, unlike the C++ version, but assuming all Objective-C exceptions are fatal, I'm not worried about that.

UPDATE Just knocked-up a quick test, and it appears to be working fine. See this gist.


Viewing all articles
Browse latest Browse all 226

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>