当优化(O1或O2或O3)打开时,程序被困在pthread_spin_unlock语句中



我正在编写一个有8个线程的程序。我正在实现一个屏障,它有一个全局计数,当它拥有锁时,每个线程都会递增该计数。所有线程都在while循环中等待该计数变为8,当它变为8时,它们应该继续。我看到,只有从7计数到8的线程最终会继续进行,而所有其他线程都停留在增量之后的unlock语句上。所有这些只有当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的线程是唯一一个达到这个断点的线程。当我使用"infothreads"查看其他线程的状态时,所有线程都被困在pthreadspinunlock语句中。

如有任何帮助,我们将不胜感激。

添加

//global declaration
pthread_spinlock_t lcl_mutex_1
//in main
pthread_spin_init (&lcl_mutex_1, 0);

我使用g++-DUSE_SPINLOCK-O3-g corr_coeff_paralle_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来实现这一点,而另一个线程可能正在修改它。最简单的修复方法是在读取过程中保持自旋锁。另一种解决方案是使用一个库(或编译器特定的内在代码或汇编代码),该库提供具有定义的线程间内存可见性语义的原子内存操作。

最新更新