ksnowlv

回顾过去,总结以往;立足现在,铭记当下;技术为主,笔记而已.

Mac下NSDistributedLock同步性能

| Comments

Mac下NSDistributedLock也可以用来同步。

  • 可以用来限制对某些共享资源的访问,比如一个文件。
  • 没有实现NSLocking协议,所有没有lock方法。一个lock方法将会阻塞线程的执行,并要求系统以预定的速度轮询锁。
  • 它提供了一个tryLock方法,并让你决定是否轮询。访问结束后,可以通过调用unlock方法来释放它。

我们使用该锁对doucument目录访问。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    ...
    NSString *doucumentDir = [paths objectAtIndex:0];
    NSLog(@"doucumentDir = %@", doucumentDir);

    const NSInteger KRunTimes = 1000 * 1000;
    double curTime, lastTime;

    // NSDistributedLock
    NSDistributedLock *lock = [NSDistributedLock lockWithPath:doucumentDir];
    lastTime = CFAbsoluteTimeGetCurrent();

    for (NSInteger i = 0; i < KRunTimes; ++i) {

        if ([lock tryLock]) {
            [lock unlock];
        }
    }

    curTime = CFAbsoluteTimeGetCurrent();
    NSLog(@"@synchronized: %f ms", (curTime - lastTime) * 1000);

日志输出:

2014-09-07 14:55:32.438 NSDistributedLockTest[4131:303] doucumentDir = /Users/ksnowlv/Documents
2014-09-07 14:55:33.703 NSDistributedLockTest[4131:303] @synchronized: 1264.391005 ms

结论:和其它同步锁相比,耗时是比较高的。

Comments

comments powered by Disqus
Included file 'custom/after_footer.html' not found in _includes directory