简体   繁体   English

与Linux相比,为什么Mac OS X上的pthread_mutex性能如此糟糕?

[英]Why is performance of pthread_mutex so bad on Mac OS X compared to Linux?

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. 我现在正在学习多线程编程,并且我注意到在Mac OS X上使用互斥锁实现同步的程序非常慢,在某种程度上,通常最好使用单线程。 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);

}

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. 该程序在装有4GB RAM和Intel Core i5 Dual Core 1.6GHz处理器的MacBook Air(OS X El Capitan)上运行了11.022169秒。 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. 在装有Ubuntu 14.04、16GB RAM和Intel Core 17 Octal Core 2.4GHz处理器的另一台计算机上,它仅运行了0.493699秒。 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. 如果有人能为我提供一些有关这种差异的原因的知识,我将不胜感激。

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. 在Linux上,自旋锁比具有大量线程的互斥锁要慢得多,而在Mac OS X上,互斥锁总是要慢得多,相差一到两位数。

Mutexes by default on MacOS apparently are implemented as "fair" mutexes. 在MacOS上默认情况下,互斥体显然是作为“一般”互斥体实现的。 The downside of this can be significantly reduced performance; 不利的一面是会大大降低性能。 see https://blog.mozilla.org/nfroyd/2017/03/29/on-mutex-performance-part-1/ 参见https://blog.mozilla.org/nfroyd/2017/03/29/on-mutex-performance-part-1/

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 请注意,当Mozilla尝试在Mac上切换至FIRSTFIT(未记录!)时,我们发现了阻止它的问题: https ://bugzilla.mozilla.org/show_bug.cgi?id=1353787#c7

On Linux, mutexes are generally implemented in terms of the futex system call. 在Linux上,互斥锁通常是根据futex系统调用来实现的。 On OS X, locking is significantly more costly because it requires sending a message to the kernel. 在OS X上,锁定的成本要高得多,因为它需要向内核发送消息。 However, this is notoriously difficult to benchmark correctly, and I haven't examined your code. 但是,众所周知,这很难正确进行基准测试,而且我还没有检查您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM