Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use multiple omp_lock_t's in OpenMP?

I assumed that I would be able to create 2 different omp_lock_t's, and lock them independently of each other. I tested the following section of code with gcc 4.4 and gcc 4.6.1 and got the same output:

omp_lock_t lockA;
omp_lock_t lockB;
omp_init_lock(&lockA);
omp_init_lock(&lockB);

std::cout << "Lock B is: " omp_test_lock(&lockB) << "\n";
omp_set_lock(&lockA);
std::cout << "Lock A set\n";
std::cout << "Lock B is: " omp_test_lock(&lockB) << "\n";

omp_set_lock(&lockB);
...

This code produces the following output:

Lock B is: 1
Lock A set
Lock B is: 0

then it deadlocks on the omp_set_lock(&lockB) attempt.

Is it not possible to create two different locks and use them independently? If it is possible, what is the correct way to set up these locks?

Thanks

like image 836
selecsosi Avatar asked Dec 30 '25 00:12

selecsosi


1 Answers

The code works as expected. I’m assuming that you have a misunderstanding about what omp_test_lock does (especially since the name is admittedly very misleading).

omp_test_lock tries to acquire a lock. But unless omp_set_lock, it doesn’t block until it has successfully acquired the lock. But that’s the only difference. Notably, after the first “test” (which isn’t just a test), lockB is acquired, that’s why the second “test” fails, and why the subsequent omp_set_lock(&lockB) deadlocks.

And just to make this clear: yes, you can use multiple locks.

omp_lock_t lockA;
omp_lock_t lockB;
omp_init_lock(&lockA);
omp_init_lock(&lockB);

omp_set_lock(&lockA);
std::cout << "Lock A set\n";
omp_set_lock(&lockB);
std::cout << "Lock B set\n";
…

This code won’t deadlock.

like image 142
Konrad Rudolph Avatar answered Dec 31 '25 13:12

Konrad Rudolph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!