![](/img/trans.png)
[英]-O3 in ICC messes up intrinsics, fine with -O1 or -O2 or with corresponding manual assembly
[英]program is getting stuck at pthread_spin_unlock statement when optimizations (O1 or O2 or O3) are turned on
我正在编写一个有8个线程的程序。 我正在实现一个具有全局计数的屏障,当它拥有锁时,每个线程都会增加该计数。 所有线程都在while循环中等待该计数变为8,当计数变为8时,它们应该继续进行。 我看到只有计数从7到8的线程才真正结束执行,而所有其他线程都在递增之后的解锁语句中停留。 所有这些仅在O1,O2或O3优化中的任何一个打开时发生。
代码是
// some code
pthread_spin_lock (&lcl_mutex_1);
sync_count_1++; // global count
pthread_spin_unlock (&lcl_mutex_1);
while (isbreak_1 == 0) {
if (sync_count_1==8) {
cout << a << endl; //a is argument that indicated the thread number.
isbreak_1=1;
}
}
// some code
如果未启用任何优化,则整个过程都可以正常工作。
这是我验证的内容。 我用-O3和-g编译。 在
cout << a << endl;
线。 我看到将计数更新为8的线程是唯一达到此断点的线程。 当我使用“信息线程”查看其他线程的状态时,所有这些线程都停留在pthread_spin_unlock语句中。
任何解决此问题的帮助将不胜感激。
新增中
//global declaration
pthread_spinlock_t lcl_mutex_1
//in main
pthread_spin_init (&lcl_mutex_1, 0);
我使用g ++ -DUSE_SPINLOCK -O3 -g corr_coeff_parallel_v9.cpp -lpthread编译了代码
我也将复制并粘贴gdb输出
[Thread debugging using libthread_db enabled]
[New Thread 0x40a00940 (LWP 30485)]
[New Thread 0x41401940 (LWP 30486)]
[New Thread 0x41e02940 (LWP 30487)]
[New Thread 0x42803940 (LWP 30488)]
[New Thread 0x43204940 (LWP 30489)]
[New Thread 0x43c05940 (LWP 30490)]
[New Thread 0x44606940 (LWP 30491)]
[New Thread 0x45007940 (LWP 30492)]
Time is 53 0 //these are some time measurements I have made before the prolematic section
Time is 51 1
Time is 51 4
Time is 51 5
Time is 51 2
Time is 51 6
Time is 51 3
[Thread 0x2aaaaaabfc10 (LWP 30482) exited]
[Switching to Thread 0x44606940 (LWP 30491)]
Breakpoint 1, calc_corr (t=0x6) at corr_coeff_parallel_v9.cpp:337
337 cout << a << endl;
(gdb) info threads
9 Thread 0x45007940 (LWP 30492) 0x00000000004033e4 in calc_corr (t=0x7) at corr_coeff_parallel_v9.cpp:334
* 8 Thread 0x44606940 (LWP 30491) calc_corr (t=0x6) at corr_coeff_parallel_v9.cpp:337
7 Thread 0x43c05940 (LWP 30490) 0x00000000004033e4 in calc_corr (t=0x5) at corr_coeff_parallel_v9.cpp:334
6 Thread 0x43204940 (LWP 30489) 0x00000000004033e4 in calc_corr (t=0x4) at corr_coeff_parallel_v9.cpp:334
5 Thread 0x42803940 (LWP 30488) 0x00000000004033e4 in calc_corr (t=0x3) at corr_coeff_parallel_v9.cpp:334
4 Thread 0x41e02940 (LWP 30487) 0x00000000004033e4 in calc_corr (t=0x2) at corr_coeff_parallel_v9.cpp:334
3 Thread 0x41401940 (LWP 30486) 0x00000000004033e4 in calc_corr (t=0x1) at corr_coeff_parallel_v9.cpp:334
2 Thread 0x40a00940 (LWP 30485) 0x00000000004033e4 in calc_corr (t=0x0) at corr_coeff_parallel_v9.cpp:334
(gdb)
POSIX标准允许在一个线程中访问一个对象,而另一个线程正在或可能正在修改该对象的未定义行为。 您的代码通过在while
循环中访问sync_count_1
而另一个线程可能正在对其进行修改来做到这一点。 最简单的解决方法是在读取过程中保持自旋锁。 另一种解决方案是使用一个库(或特定于编译器的内部代码或汇编代码),该库提供具有定义的线程间内存可见性语义的原子内存操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.