omp_test_nest_lock
尝试设置可套上的锁定,但不阻塞线程上执行。
int omp_test_nest_lock(
omp_nest_lock_t *lock
);
备注
其中,
- lock
初始化 omp_init_nest_lock类型 omp_nest_lock_t 的变量。
备注
有关更多信息,请参见 3.2.5 omp_test_lock和omp_test_nest_lock功能。
示例
// omp_test_nest_lock.cpp
// compile with: /openmp
#include <stdio.h>
#include <omp.h>
omp_nest_lock_t nestable_lock;
int main() {
omp_init_nest_lock(&nestable_lock);
#pragma omp parallel num_threads(4)
{
int tid = omp_get_thread_num();
while (!omp_test_nest_lock(&nestable_lock))
printf_s("Thread %d - failed to acquire nestable_lock\n",
tid);
printf_s("Thread %d - acquired nestable_lock\n", tid);
if (omp_test_nest_lock(&nestable_lock)) {
printf_s("Thread %d - acquired nestable_lock again\n",
tid);
printf_s("Thread %d - released nestable_lock\n",
tid);
omp_unset_nest_lock(&nestable_lock);
}
printf_s("Thread %d - released nestable_lock\n", tid);
omp_unset_nest_lock(&nestable_lock);
}
omp_destroy_nest_lock(&nestable_lock);
}