• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSLock and threads.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Prev by Date: Re: New document does NOT open at program start!
  • Next by Date: Re: NSImage leaks on every draw?! Help!
  • Previous by thread: Re: Runtime messages
  • Next by thread: Multi-nib communication
  • Index(es):
    • Date
    • Thread