Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 与pthreads不一致的运行时_C_Linux_Multithreading_Synchronization_Pthreads - Fatal编程技术网

C 与pthreads不一致的运行时

C 与pthreads不一致的运行时,c,linux,multithreading,synchronization,pthreads,C,Linux,Multithreading,Synchronization,Pthreads,我的多线程C程序运行以下例程: #define NUM_LOOP 500000000 long long sum = 0; void* add_offset(void *n){ int offset = *(int*)n; for(int i = 0; i<NUM_LOOP; i++) sum += offset; pthread_exit(NULL); } 输出和运行时间为: sum = 500000000 real 0m0.6

我的多线程C程序运行以下例程:

#define NUM_LOOP 500000000
long long sum = 0;

void* add_offset(void *n){
        int offset = *(int*)n;
        for(int i = 0; i<NUM_LOOP; i++) sum += offset;
        pthread_exit(NULL);
}
输出和运行时间为:

sum = 500000000

real    0m0.686s
user    0m0.680s
sys     0m0.000s
当主功能为(多线程顺序)时:

输出和运行时间为:

sum = 0

real    0m1.362s
user    0m1.356s
sys     0m0.000s
sum = 166845932

real    0m2.087s
user    0m3.876s
sys     0m0.004s
到目前为止,该程序按预期运行。但当主函数为(多线程并发)时:

输出和运行时间为:

sum = 0

real    0m1.362s
user    0m1.356s
sys     0m0.000s
sum = 166845932

real    0m2.087s
user    0m3.876s
sys     0m0.004s
由于缺少同步而导致的
sum
的错误值不是这里的问题,而是运行时间。并发执行的实际运行时间远远超过顺序执行。它与多核CPU中并发执行的预期相反


请解释一下这里可能有什么问题

如果多个线程访问同一个共享状态(至少在x86上),这种情况并不少见。它通常被称为:

每当一个内核想要更新该变量的值时,它首先必须从另一个内核获取缓存线的“所有权”(锁定缓存线以便写入),这需要一些时间。然后另一个核心想要回缓存线


因此,即使没有同步原语,与顺序情况相比,您也要付出巨大的开销。

正如@spectras所建议的,我对
添加偏移量
例程进行了以下更改:

#define NUM_LOOP 500000000
long long sum = 0;

void* add_offset(void *n){
        int offset = *(int*)n;
        long long sum_local = sum; //read sum
        for(int i = 0; i<NUM_LOOP; i++) sum_local += offset;
        sum = sum_local; //write to sum
        pthread_exit(NULL);
}
另一个输出和运行时是:

sum = -500000000

real    0m0.686s
user    0m1.360s
sys     0m0.000s

由于线程不同步,因此需要输出的这两个值,而且只有这两个值。输出中的
sum
值反映了最后更新了
sum
的线程(偏移量为1或偏移量为-1)

你看过汇编程序中的三种变体了吗?有什么区别是你无法解释的吗?您是否在任何地方都使用了相同的优化?您使用了多少优化?你使用了哪个编译器,Linux的哪个版本/变体?1)gcc(Ubuntu 5.4.0-6ubuntu1~16.04.4)5.4.0 20160609 2)我只是用gcc-std=c99-pthread编译它3)我使用Ubuntu 16.04.2,内核:4.4.0-79-genericThanks作为缓存线ping的链接-pong@Aroonalok>就这个例子来说,共享状态通常很昂贵,应该避免。例如,您可以通过让每个线程在本地计算一个和,并且只在最后更新共享状态来避免这种开销。
sum = 500000000

real    0m0.683s
user    0m1.356s
sys     0m0.000s
sum = -500000000

real    0m0.686s
user    0m1.360s
sys     0m0.000s