【问题标题】:VC++: crash when freeing a DLL built with openMPVC++:释放使用 openMP 构建的 DLL 时崩溃
【发布时间】:2015-12-23 16:28:32
【问题描述】:

我已经减少了以下玩具代码的崩溃:

// 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"));
   }
}

_

// 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;
}

这是一个 MS 错误吗?是否有一些我错过的 OMP 线程清理 API(probably not,但也许)?我手头没有其他编译器。他们是否以不同的方式对待这种情况? (再次,probably not)OMP 标准对这种情况有什么要说的吗?

【问题讨论】:

  • Unloaded 'C:\Windows\SysWOW64\vcomp100d.dll' 之后在VS2010 中尝试了您的代码,在return 0 之后,我确实遇到了一些访问冲突。但是当注释掉FreeLibrary 时,不会出现访问冲突。

标签: visual-c++ openmp msvcrt


【解决方案1】:

我收到了an answer from Eric Brumer @ MS Connect。在这里重新发布,以防将来有人感兴趣:

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

你可以告诉 openmp 不要旋转等待,线程会立即 循环结束后阻塞。只需设置 OMP_WAIT_POLICY=passive 您的环境,或调用 SetEnvironmentVariable(L"OMP_WAIT_POLICY", L“被动”);在加载dll之前在你的函数中。默认是 “active”告诉线程池旋转等待。使用环境 变量,或者在调用 FreeLibrary 之前等待几秒钟。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多