版权声明: https://blog.csdn.net/henysugar/article/details/84107336
因为自旋锁有风险已经别踢出局不再使用,所以对比了一下pthread提供的pthread_mutex_t以及dispatch_semaphore。
测试时候特别注意debug模式和release模式,结果可能会完全不一样。
测试方法
模拟实际使用的线程抢占,分别在不同线程循环很多次调用锁和解锁,计算两条线程都完成后的时间。
测试结果
debug模式下:
pthread_mutex_t : 79秒
dispatch_semaphore:85秒
release模式下:
pthread_mutex_t : 74秒
dispatch_semaphore:54秒
结论
dispatch_semaphore是pthread_mutex_t的1.37倍左右。
以release的为准,因为我们的程序最终是以release模式运行。
本文结束。