Reputation: 181
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
Upvotes: 3
Views: 2072
Reputation: 545865
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.
Upvotes: 4