动态库中使用openmp 发生异常报错

  近期动态库中使用openmp,项目调用导致出错,查找许久没找到原因。项目中使用的接口也是奇怪,写了个demo程序模拟,发现有可能是因为动态库中使用的并发资源还未释放掉,动态库就被释放了,导致出错。现在将demo代码贴出,在析构函数内添加sleep函数或者在释放掉动态库之前调用sleep函数,现在贴出代码仅供参考:


主程序 :

// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "windows.h"
#include <iostream>
#include "dll.h"
#include <omp.h>
using namespace std;

typedef void(*getChars)(HANDLE handl);
typedef MyClass*(*getClassAddr)();
typedef void(*deleteClass)(HANDLE handl);

void test1()
{
	char *p = new char[256];
	HINSTANCE m_hDec = LoadLibraryA("ConsoleApplicationdll.dll");
	if (!m_hDec)
	{
		cout << "1  " << m_hDec<< endl;
		return;
	}
	getClassAddr pAddr = (getClassAddr)GetProcAddress(m_hDec, "create");
	if (!pAddr)
	{
		cout << "2" << endl;
		return;
	}
	getChars getchars = (getChars)GetProcAddress(m_hDec, "getchars");
	if (!getchars)
	{
		cout << "3" << endl;
		return;
	}
	deleteClass del = (deleteClass)GetProcAddress(m_hDec, "deleteclass");
	if (!del)
	{
		cout << "4" << endl;
		return;
	}
	MyClass *myclass = pAddr();
	getchars(myclass);

	cout << p << endl;
	del(myclass);
	
	Sleep(200);//此处添加sleep函数也可以
FreeLibrary(m_hDec);}int _tmain(int argc, _TCHAR* argv[]){test1();getchar();return 0;}



动态库代码:

dll.h

#pragma  once
#include <windows.h>

#define DllExport	__declspec(dllexport)

class  MyClass
{
public:
	MyClass();
	~MyClass();

	void getchars(char*p);
	void unmount();
private:
	void test( long, bool &);
	
};


dll.cpp

#include "stdafx.h"
#include <omp.h>
#include "dll.h"
MyClass::MyClass()
{	
}

MyClass::~MyClass()
{
//	Sleep(200);//此处添加sleep函数可以
}

void testt()
{
	WORD bFindAFileHead = 0;
#pragma omp parallel for 
	for (long i = 0; i < 100000000; ++i)//只有匹配头部的时候才会同一个扇区都去匹配文件头部
	{
		bFindAFileHead++;
	}

#pragma omp barrier

}

void MyClass::getchars(char*p)
{
	bool bflag = false;
	if (p == nullptr)
	{
		p = new char[100];
	}

	bool bFindAFileHead = false;


#pragma omp parallel for 
	for (long i = 0; i < 1000000; ++i)
	{
		bFindAFileHead = false;
		if (!bFindAFileHead)
		{
			//test(i, bFindAFileHead);
		}
	}
	//	}
#pragma omp barrier

#pragma omp single
	memcpy(p/**p*/, "asdfasdfasdfasdfasdfasdf", 50);

}

void MyClass::test(long i, bool &bFindAFileHead)
{
	bool bflag = false;
	for (long k = 0; k < 100000000; ++k)
	{
		if (k == 5000)
		{
			bflag = true;
			break;
		}
	}
}

void MyClass::unmount()
{
	delete this;
}


ConsoleApplicationdll.cpp

// ConsoleApplicationdll.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "dll.h"
#include <omp.h>

extern "C" DllExport MyClass* create()
{
	return  new MyClass();
}

extern "C" DllExport void getchars(HANDLE handl)
{
	MyClass *pp = (MyClass*)handl;
	char *p = new char[256];
	pp->getchars(p);	
}


extern "C" DllExport void deleteclass(HANDLE handl)
{
	MyClass *pp = (MyClass*)handl;
	pp->unmount();
}


demo代码
http://pan.baidu.com/s/1jISfH7S

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值