Last active
August 29, 2015 13:57
-
-
Save 3cooper/9812665 to your computer and use it in GitHub Desktop.
Question for /r/learnprogramming about thread safety
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation test | |
{ | |
NSData *current; | |
NSData *next; | |
} | |
//incoming data calls this method on thread A | |
- (void)gotNewData(NSData *)data | |
{ | |
/// lock next here? | |
next = data; | |
/// unlock next here? | |
} | |
//thread B sends data through this method | |
- (void)sendData | |
{ | |
/// lock next here? | |
if( !current && next ) | |
{ | |
current = [next copy] | |
next = nil; | |
} | |
/// unlock next here? | |
if( current ) | |
{ | |
if( [self doSomethingWith:current] ) | |
{ | |
current = nil; | |
} | |
} | |
} | |
- (void)doSomethingWith:(NSData *)data | |
{ | |
BOOL done; | |
// process part of data using a counter | |
// to move along with each call | |
// if end of data is reached in this call | |
// return NO else return YES | |
return done; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment