NSLock and threads.
NSLock and threads.
- Subject: NSLock and threads.
- From: Anthony Duer <email@hidden>
- Date: Fri, 25 Oct 2002 13:46:35 -0700
I currently have a second thread dedicated to updating a part of the
user interface which requires new data to occasionally be dispatched
into it. I think the safest way to do this is via NSLocks so there's no
problems with synchronization between the two threads. I'm pretty sure
it should look like the code below, but just wanted to double check.This
is somewhat pseudo coded, but should be readable. =)
-(void)UIThread
{
while(iAmConnected == YES)
{
[myLock lock]
if([myMutableArray count] > 0)
{
NSString *gsPart = [[myMutableArray objectAtIndex: 0]
substringFromIndex: 4];
[myMutableArray removeObjectAtIndex: 0];
[myLock unlock];
}
else
{
[myLock unlock]
[NSThread sleepUntilDate: [NSDate +0.1 seconds]];
}
}
}
-(void)updateData:(NSString *)data
{
[myLock lock];
[myMutableArray addObject: data];
[myLock unlock];
}
~Anthony Duer
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.