diff --git a/openmp/runtime/src/kmp_runtime.cpp b/openmp/runtime/src/kmp_runtime.cpp index c8a18e81810cb..a1e36243f750c 100644 --- a/openmp/runtime/src/kmp_runtime.cpp +++ b/openmp/runtime/src/kmp_runtime.cpp @@ -196,6 +196,12 @@ int __kmp_get_global_thread_id() { if (i < 0) return i; + // other_threads[i] can be nullptr at this point because the corresponding + // thread could have already been destructed. It can happen when this function + // is called in end library routine. + if (!TCR_SYNC_PTR(other_threads[i])) + return i; + /* dynamically updated stack window for uber threads to avoid get_specific call */ if (!TCR_4(other_threads[i]->th.th_info.ds.ds_stackgrow)) {