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

时间:2012-02-24 23:47:22

标签: c++ optimization synchronization pthreads compiler-optimization

我正在编写一个有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的线程是唯一一个达到此断点的线程。当我使用“info threads”来查看其他线程的状态时,所有这些线程都停留在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) 

1 个答案:

答案 0 :(得分:1)

POSIX标准使得访问一个线程中的对象,而另一个线程正在或可能正在修改它未定义的行为。您的代码通过访问sync_count_1循环中的while来执行此操作,而另一个线程可能正在修改它。最简单的解决方法是在读取期间保持自旋锁。另一种解决方案是使用库(或特定于编译器的内部或汇编代码),它提供具有定义的线程内存可见性语义的原子内存操作。