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

时间:2015-12-23 16:28:32

标签: 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 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标准对这种情况有什么要说的吗?

1 个答案:

答案 0 :(得分:3)

我得到了an answer from Eric Brumer @ MS Connect。如果将来任何人都感兴趣,请在此处重新发布:

  

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

     

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