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
The code works as expected. I’m assuming that you have a misunderstanding about what
omp_test_lockdoes (especially since the name is admittedly very misleading).omp_test_locktries to acquire a lock. But unlessomp_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),lockBis acquired, that’s why the second “test” fails, and why the subsequentomp_set_lock(&lockB)deadlocks.And just to make this clear: yes, you can use multiple locks.
This code won’t deadlock.