与Linux相比,为什么Mac OS X上的pthread_mutex性能如此糟糕? [英] Why is performance of pthread_mutex so bad on Mac OS X compared to Linux?
问题描述
我现在正在学习多线程编程,我注意到在Mac OS X上使用互斥锁实现同步的程序非常慢,在某种程度上,通常最好使用单线程.我知道有很多更快的同步方法,但是我仍然想知道为什么会这样.为了进行简单的时间测量,我编写了该程序.
I am learning about multi-thread programming right now, and I noticed that programs with synchronization implemented with mutex is extremely slow on Mac OS X, to the extent it is usually better to use single thread instead. I understand that there are much faster ways of synchronizing, but I still wonder why it is like this. For a simple time measurement, I wrote this program.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
pthread_mutex_t lock;
long s;
double cur_time() {
struct timeval tp[1];
gettimeofday(tp, NULL);
return tp->tv_sec + tp->tv_usec * 1.0E-6;
}
void * func(){
int n = 1000000;
while(n > 0){
pthread_mutex_lock(&lock);
s++;
n --;
pthread_mutex_unlock(&lock);
}
return 0;
}
void * thread_func(void * arg_){
return func();
}
int main(){
pthread_mutex_init(&lock,NULL);
s = 0;
int i;
pthread_t pids[3];
double t1 = cur_time();
for(i = 0; i < 3; i++){
pthread_create(&pids[i],NULL,thread_func,NULL);
}
for(i = 0; i < 3; i++){
pthread_join(pids[i],0);
}
printf("s = %ld\n",s);
double t2 = cur_time();
printf("Time consumed: %fs\n",t2 - t1);
}
该程序在装有4GB RAM和Intel Core i5 Dual Core 1.6GHz处理器的MacBook Air(OS X El Capitan)上运行了11.022169秒.在装有Ubuntu 14.04、16GB RAM和Intel Core 17 Octal Core 2.4GHz处理器的另一台计算机上,它仅运行了0.493699秒.我知道这两台计算机之间的处理能力有很大的差异,但是我不希望两者之间的差异如此之大.此外,当使用其他锁(例如自旋锁)时,差别永远不会那么大.
This program ran for 11.022169 seconds on my MacBook Air (OS X El Capitan), which has 4GB RAM and a Intel Core i5 Dual Core 1.6GHz processor. It only ran for 0.493699 seconds on my another computer with Ubuntu 14.04, a 16GB RAM, and a Intel Core 17 Octal Core 2.4GHz processor. I understand that there is a significant difference in processing power between these two computers, but I would not expect the difference to be this huge. Besides, when using other locks, for example spinlocks, the difference is never this big.
如果有人能为我提供一些造成这种差异的原因的知识,我将不胜感激.
I would be very grateful if someone could offer me some knowledge on the reason of this difference.
添加:我错过了一些东西.我还分别比较了每个操作系统上的自旋锁和互斥锁.在Linux上,自旋锁比具有大量线程的互斥锁要慢得多,而在Mac OS X上,互斥锁总是要慢得多.相差一到两位数.
Added: I missed out something. I also compared spinlock and mutex on each OS respectively. While on Linux the spinlock is significantly slower than mutex with a large number of threads, on Mac OS X mutex is always much much slower.To the extent of difference by one or two digits.
推荐答案
在MacOS上默认情况下,互斥体显然是作为公平"互斥体实现的.不利的一面是会大大降低性能.请参阅 https://blog.mozilla. org/nfroyd/2017/03/29/on-mutex-performance-part-1/
Mutexes by default on MacOS apparently are implemented as "fair" mutexes. The downside of this can be significantly reduced performance; see https://blog.mozilla.org/nfroyd/2017/03/29/on-mutex-performance-part-1/
请注意,当Mozilla尝试在Mac上切换到FIRSTFIT(未记录!)时,我们发现了阻止它的问题:
Note that when Mozilla tried switching to FIRSTFIT on mac (which isn't documented!), we found problems that blocked it: https://bugzilla.mozilla.org/show_bug.cgi?id=1353787#c7
这篇关于与Linux相比,为什么Mac OS X上的pthread_mutex性能如此糟糕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!