Visual c++ VC++;:释放使用openMP构建的DLL时崩溃

Visual c++ VC++;:释放使用openMP构建的DLL时崩溃,visual-c++,openmp,msvcrt,Visual C++,Openmp,Msvcrt,我已将崩溃简化为以下玩具代码: // DLLwithOMP.cpp : build into a dll *with* /openmp #include <tchar.h> extern "C" { __declspec(dllexport) void funcOMP() { #pragma omp parallel for for (int i = 0; i < 100; i++) _tprintf(_T("Please fondle m

我已将崩溃简化为以下玩具代码:

// DLLwithOMP.cpp : build into a dll *with* /openmp
#include <tchar.h>
extern "C"
{
   __declspec(dllexport)  void funcOMP()
   {
#pragma omp parallel for
    for (int i = 0; i < 100; i++)
        _tprintf(_T("Please fondle my buttocks\n"));
   }
}
//DLLwithOMP.cpp:使用*/openmp构建到dll*中
#包括
外部“C”
{
__declspec(dllexport)void funcOMP()
{
#pragma-omp并行
对于(int i=0;i<100;i++)
_tprintf(_T(“请抚摸我的屁股”);
}
}
_

//ConsoleApplication1.cpp:构建到不带*/openmp的可执行文件*中
#包括
#包括
#包括
typedef void(*tDllFunc)();
int main()
{
HMODULE hDLL=LoadLibrary(_T(“DLLwithOMP.dll”);
tDllFunc pDllFunc=(tDllFunc)GetProcAddress(hDLL,“funcOMP”);
pDllFunc();
免费图书馆(hDLL);
//此时,omp运行时vcomp140[d].dll refcount为零
//windows会卸载它,但omp线程团队仍处于活动状态。
//通常会发生撞车事故。
返回0;
}
这是微软的错误吗?是否有一些OMP线程清理API我错过了(,但可能)?我手头没有其他编译器。他们对这种情况有不同的看法吗?(再次强调)OMP标准对这种情况有何评论

我明白了。如果将来有人对其感兴趣,请在此重新发布:

为了获得最佳性能,openmp线程池自旋将等待大约 第二次停机前,以防有更多工作可用。如果 如果卸载一个正在旋转等待的DLL,它将崩溃 以你看到的方式(大部分时间)

您可以告诉openmp不要旋转等待,线程将立即停止 循环完成后阻塞。只需在中设置OMP\u WAIT\u POLICY=passive 您的环境,或调用SetEnvironmentVariable(L“OMP_WAIT_POLICY”, L“被动”);在加载dll之前,在函数中。默认值是 “活动”指示线程池旋转等待。利用环境 变量,或在调用FreeLibrary之前等待几秒钟

我知道了。如果将来有人对其感兴趣,请在此重新发布:

为了获得最佳性能,openmp线程池自旋将等待大约 第二次停机前,以防有更多工作可用。如果 如果卸载一个正在旋转等待的DLL,它将崩溃 以你看到的方式(大部分时间)

您可以告诉openmp不要旋转等待,线程将立即停止 循环完成后阻塞。只需在中设置OMP\u WAIT\u POLICY=passive 您的环境,或调用SetEnvironmentVariable(L“OMP_WAIT_POLICY”, L“被动”);在加载dll之前,在函数中。默认值是 “活动”指示线程池旋转等待。利用环境 变量,或在调用FreeLibrary之前等待几秒钟


在VS2010中尝试了您的代码,在
卸载了'C:\Windows\SysWOW64\vcomp100d.dll'
之后,在
返回0
之后,我确实遇到了一些访问冲突。但是当注释掉
免费库时,则没有访问冲突。在
卸载'C:\Windows\SysWOW64\vcomp100d.dll'
后,在VS2010中尝试了您的代码,在
返回0
后,我确实遇到了一些访问冲突。但是当注释掉
免费库
时,则没有访问冲突。
// ConsoleApplication1.cpp : build into an executable *without* /openmp

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

typedef void(*tDllFunc) ();

int main()
{
    HMODULE hDLL = LoadLibrary(_T("DLLwithOMP.dll"));
    tDllFunc pDllFunc = (tDllFunc)GetProcAddress(hDLL, "funcOMP");
    pDllFunc();
    FreeLibrary(hDLL);
    // At this point the omp runtime vcomp140[d].dll refcount is zero 
    // and windows unloads it, but the omp thread team remains active.
    // A crash usually ensues.
    return 0;
}