Skip to content

Instantly share code, notes, and snippets.

@3cooper
Last active August 29, 2015 13:57
Show Gist options
  • Save 3cooper/9812665 to your computer and use it in GitHub Desktop.
Save 3cooper/9812665 to your computer and use it in GitHub Desktop.
Question for /r/learnprogramming about thread safety
@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