我有一个懒惰的方案,我有以下代码:
class C
{
foo(); //for every object C foo() (read/write) has to be called once before any bar()
bar(); //bar is read only and completely threadsafe
was_foo_already_called(); // true if any thread already called foo()
was_foo_already_finished(); // true if any thread already called and finished foo()
int ID; //class has members (is not empty)
}
...
const int large_number = 100;
vector<C> vec (1000);
#pragma omp parallel for
for (int i = 0; i < large_number ; i++) {
int x = unknown_number_between_0_and_999( i ); //unforseeable index for vec
C& obj = vec.at(x);
if ( obj.was_foo_already_finished() ) {
obj.bar(); // (Case 1) quick and threadsafe
} else if ( obj.was_foo_already_called() ) {
(...) // (Case 2) needs to be blocked somehow
obj.bar();
} else {
obj.foo(); // (Case 3)
obj.bar();
}
}
案例3本身就很好(只要两个布尔测试都使用同步,例如使用范围锁定),情况1也是如此。
现在我的问题是案例2.即使我使用范围锁定只锁定C的一个实例,只要was_foo_already_finished()
返回false,情况2就会被阻止,它仍会阻止所有具有该x的线程,除了一个,在最坏的情况下只留下1个核心。
我需要一个实现,其他&#34;免费&#34;核心只是继续下一次迭代(稍后返回他们放弃的早期迭代,有时希望foo()
完成)。
OpenMP中是否有一个隐含的功能,我只是在监督?
PS:我的整个代码不包含OpenMP提供的任何特定同步工具,这是问题的另一个(较小的)部分:如何最好地同步这些代码部分?