double checked locking - objective c

Posted by bandejapaisa on Stack Overflow See other posts from Stack Overflow or by bandejapaisa
Published on 2010-06-07T14:43:37Z Indexed on 2010/06/07 14:52 UTC
Read the original article Hit count: 453

I realised double checked locking is flawed in java due to the memory model, but that is usually associated with the singleton pattern and optimizing the creation of the singleton.

What about under this case in objective-c:

I have a boolean flag to determine if my application is streaming data or not. I have 3 methods, startStreaming, stopStreaming, streamingDataReceived and i protect them from multiple threads using:

- (void) streamingDataReceived:(StreamingData *)streamingData {
    if (self.isStreaming) {
        @synchronized(self) {
            if (self.isStreaming) {

- (void) stopStreaming {
    if (self.isStreaming) {
        @synchronized(self) {
            if (self.isStreaming) {

- (void) startStreaming:(NSArray *)watchlistInstrumentData {
    if (!self.isStreaming) {
        @synchronized(self) {
            if (!self.isStreaming) {

Is this double check uneccessary? Does the double check have similar problems in objective-c as in java? What are the alternatives to this pattern (anti-pattern).

Thanks

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about locking