• 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
Re: NSDistributedLock question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDistributedLock question


  • Subject: Re: NSDistributedLock question
  • From: Shaun Wexler <email@hidden>
  • Date: Wed, 19 Apr 2006 14:56:05 -0700

On Apr 19, 2006, at 12:51 PM, Bill wrote:

I'm having some trouble using NSDistributedLock, and maybe someone can shed some light.
In the Finder, I first create a new sample text file on my hard disk. My application then does this:


// filename is the path to the text file I just created
NSDistributedLock *myLock = [[NSDistributedLock alloc] initWithPath:filename];
// this works fine


NSDate *date = [myLock lockDate];
// I just created the sample text file, yet the NSDate returned is valid. Who locked the file?

You did, because you created it, and as the owner you are holding the mutex now.


BOOL success = [myLock tryLock];
// this fails. Who has locked this file I just created? The File Manager?

You were holding the lock already. It's not recursive.

[myLock unlock];
// calling this causes the file to disappear (deleted?)!

Yep.

Can anyone tell me what's going on, or what I'm doing wrong?
Thanks!
Bill

If any other process tries to [create/access] the lock (ie a file with that pathname), and it already exists, it will obey normal mutex semantics and use the existing lock; if it choses to block on that mutex, for example:


BOOL locked;
do {
int timeoutms = 0, maxwaitms = 5000;
while (!(locked = [myLock tryLock]) && (timeoutms += 100) < maxwaitms) {
usleep(100000);
}
if (!locked) {
[myLock breakLock]; // may raise
}
} while (!locked);


This will remain blocked until the current process which holds the lock releases it, and once it obtains the lock, it will unblock and continue. When no processes are holding or trying to obtain the lock, the file is deleted, atomically. This class is heuristic at best, so use with caution.
--
Shaun Wexler
MacFOH
http://www.macfoh.com



_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >NSDistributedLock question (From: "Bill" <email@hidden>)

  • Prev by Date: Re: object Allocation
  • Next by Date: Re: NSDistributedLock question
  • Previous by thread: NSDistributedLock question
  • Next by thread: Re: NSDistributedLock question
  • Index(es):
    • Date
    • Thread