Ale*_*pov 12 c linux macos multithreading pthreads
我为OS课程作业写了这篇文章,我已经完成并递交了.我昨天发布了这个问题,但是由于"学术诚实"规定,我把它推迟到提交截止日期之后.
目的是学习如何使用关键部分.有一个data数组有100个单调递增的数字,0 ... 99和40个线程,每个线程随机交换两个元素2,000,000次.一秒钟后Checker,确保每个号码只有一个(这意味着没有发生并行访问).
这是Linux时代:
real 0m5.102s
user 0m5.087s
sys 0m0.000s
Run Code Online (Sandbox Code Playgroud)
和OS X次
real 6m54.139s
user 0m41.873s
sys 6m43.792s
Run Code Online (Sandbox Code Playgroud)
我ubuntu/trusty64在运行OS X的同一台机器上运行一个流浪盒.它是一个四核i7 2.3Ghz(高达3.2Ghz)2012 rMBP.
如果我理解正确,sys是系统开销,我无法控制,即便如此,41s的用户时间表明线程可能是串行运行的.
如果需要,我可以发布所有代码,但我会发布我认为相关的位.我正在使用,pthreads因为这是Linux提供的,但我认为它们适用于OS X.
创建swapper运行swapManyTimes例程的线程:
for (int i = 0; i < NUM_THREADS; i++) {
int err = pthread_create(&(threads[i]), NULL, swapManyTimes, NULL);
}
Run Code Online (Sandbox Code Playgroud)
Swapper 线程关键部分,在for循环中运行200万次:
pthread_mutex_lock(&mutex); // begin critical section
int tmpFirst = data[first];
data[first] = data[second];
data[second] = tmpFirst;
pthread_mutex_unlock(&mutex); // end critical section
Run Code Online (Sandbox Code Playgroud)
只Checker创建一个线程,方法相同Swapper.它通过遍历data数组并使用标记与每个值对应的索引来操作true.之后,它会检查有多少索引为空.因此:
pthread_mutex_lock(&mutex);
for (int i = 0; i < DATA_SIZE; i++) {
int value = data[i];
consistency[value] = 1;
}
pthread_mutex_unlock(&mutex);
Run Code Online (Sandbox Code Playgroud)
它sleep(1)在运行while(1)循环后通过调用每秒运行一次.加入所有swapper线程后,此线程也会被取消并加入.
我很乐意提供更多信息,以帮助弄清楚为什么这在Mac上如此糟糕.我真的不寻找与代码优化的帮助,除非那是什么绊倒了OS X.我试着用它建立既clang与gcc-4.9在OS X
小智 6
MacOSX和Linux以不同的方式实现pthread,导致这种缓慢的行为.具体来说,MacOSX不使用自旋锁(根据ISO C标准,它们是可选的).使用像这样的示例,这可能会导致非常非常慢的代码性能.
我已经很好地复制了你的结果(没有清扫工):
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t Lock;
pthread_t LastThread;
int Array[100];
void *foo(void *arg)
{
pthread_t self = pthread_self();
int num_in_row = 1;
int num_streaks = 0;
double avg_strk = 0.0;
int i;
for (i = 0; i < 1000000; ++i)
{
int p1 = (int) (100.0 * rand() / (RAND_MAX - 1));
int p2 = (int) (100.0 * rand() / (RAND_MAX - 1));
pthread_mutex_lock(&Lock);
{
int tmp = Array[p1];
Array[p1] = Array[p2];
Array[p2] = tmp;
if (pthread_equal(LastThread, self))
++num_in_row;
else
{
++num_streaks;
avg_strk += (num_in_row - avg_strk) / num_streaks;
num_in_row = 1;
LastThread = self;
}
}
pthread_mutex_unlock(&Lock);
}
fprintf(stdout, "Thread exiting with avg streak length %lf\n", avg_strk);
return NULL;
}
int main(int argc, char **argv)
{
int num_threads = (argc > 1 ? atoi(argv[1]) : 40);
pthread_t thrs[num_threads];
void *ret;
int i;
if (pthread_mutex_init(&Lock, NULL))
{
perror("pthread_mutex_init failed!");
return 1;
}
for (i = 0; i < 100; ++i)
Array[i] = i;
for (i = 0; i < num_threads; ++i)
if (pthread_create(&thrs[i], NULL, foo, NULL))
{
perror("pthread create failed!");
return 1;
}
for (i = 0; i < num_threads; ++i)
if (pthread_join(thrs[i], &ret))
{
perror("pthread join failed!");
return 1;
}
/*
for (i = 0; i < 100; ++i)
printf("%d\n", Array[i]);
printf("Goodbye!\n");
*/
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在Linux(2.6.18-308.24.1.el5)服务器上英特尔(R)Xeon(R)CPU E3-1230 V2 @ 3.30GHz
[ltn@svg-dc60-t1 ~]$ time ./a.out 1
real 0m0.068s
user 0m0.068s
sys 0m0.001s
[ltn@svg-dc60-t1 ~]$ time ./a.out 2
real 0m0.378s
user 0m0.443s
sys 0m0.135s
[ltn@svg-dc60-t1 ~]$ time ./a.out 3
real 0m0.899s
user 0m0.956s
sys 0m0.941s
[ltn@svg-dc60-t1 ~]$ time ./a.out 4
real 0m1.472s
user 0m1.472s
sys 0m2.686s
[ltn@svg-dc60-t1 ~]$ time ./a.out 5
real 0m1.720s
user 0m1.660s
sys 0m4.591s
[ltn@svg-dc60-t1 ~]$ time ./a.out 40
real 0m11.245s
user 0m13.716s
sys 1m14.896s
Run Code Online (Sandbox Code Playgroud)
在我的MacBook Pro(Yosemite 10.10.2)2.6 GHz i7,16 GB内存上
john-schultzs-macbook-pro:~ jschultz$ time ./a.out 1
real 0m0.057s
user 0m0.054s
sys 0m0.002s
john-schultzs-macbook-pro:~ jschultz$ time ./a.out 2
real 0m5.684s
user 0m1.148s
sys 0m5.353s
john-schultzs-macbook-pro:~ jschultz$ time ./a.out 3
real 0m8.946s
user 0m1.967s
sys 0m8.034s
john-schultzs-macbook-pro:~ jschultz$ time ./a.out 4
real 0m11.980s
user 0m2.274s
sys 0m10.801s
john-schultzs-macbook-pro:~ jschultz$ time ./a.out 5
real 0m15.680s
user 0m3.307s
sys 0m14.158s
john-schultzs-macbook-pro:~ jschultz$ time ./a.out 40
real 2m7.377s
user 0m23.926s
sys 2m2.434s
Run Code Online (Sandbox Code Playgroud)
我花了大约4倍的挂钟时间来完成40个线程,这与Linux + gcc的旧版本相比.
注意:我更改了我的代码,每个线程执行1M交换.
它看起来像在争OSX是做了很多比Linux更多的工作.也许它比Linux更精细地交错它们?
编辑更新代码以记录线程立即重新捕获锁定的平均次数:
Linux的
[ltn@svg-dc60-t1 ~]$ time ./a.out 10
Thread exiting with avg streak length 2.103567
Thread exiting with avg streak length 2.156641
Thread exiting with avg streak length 2.101194
Thread exiting with avg streak length 2.068383
Thread exiting with avg streak length 2.110132
Thread exiting with avg streak length 2.046878
Thread exiting with avg streak length 2.087338
Thread exiting with avg streak length 2.049701
Thread exiting with avg streak length 2.041052
Thread exiting with avg streak length 2.048456
real 0m2.837s
user 0m3.012s
sys 0m16.040s
Run Code Online (Sandbox Code Playgroud)
Mac OSX
john-schultzs-macbook-pro:~ jschultz$ time ./a.out 10
Thread exiting with avg streak length 1.000000
Thread exiting with avg streak length 1.000000
Thread exiting with avg streak length 1.000000
Thread exiting with avg streak length 1.000000
Thread exiting with avg streak length 1.000000
Thread exiting with avg streak length 1.000000
Thread exiting with avg streak length 1.000000
Thread exiting with avg streak length 1.000000
Thread exiting with avg streak length 1.000000
Thread exiting with avg streak length 1.000000
real 0m34.163s
user 0m5.902s
sys 0m30.329s
Run Code Online (Sandbox Code Playgroud)
因此,OSX更均匀地共享其锁,因此具有更多的线程挂起和恢复.