vs dll,dll中使用了现场,我的锁不知道 锁哪个地方,暂时没锁,调用如果是单线程 ,应该没啥影响
// TestDll.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include "thread"
#include "mutex"
BOOL ThreadEnd = FALSE;
std::mutex mut;
int Add(int a,int b)
{
return a + b;
}
void AutoAdd(int& a)
{
ThreadEnd = FALSE;
int k = a;
a = 0;
for (int i = 1; i <= k; ++i)
{
a += i;
}
ThreadEnd = TRUE;
}
int ThreadAdd(int a)
{
std::thread *t = new std::thread(AutoAdd, std::ref(a));//线程去计算
t->join();
while (!ThreadEnd)
{
Sleep(10);
}
return a;
delete t;
}
delphi 调用
type Tadd = function (a,b: integer): Integer;cdecl; //c++ 的dll
type TthreadAdd = function (a: Integer): Integer; cdecl;
var
h: Thandle;
ladd: tadd;
ltadd: TthreadAdd;
i,k: Integer;
begin
h := LoadLibrary('TestDll.dll');
if h > 0 then
begin
ladd := GetProcAddress(h,'Add');
ltadd := GetProcAddress(h,'ThreadAdd');
i := ladd(10,13);
k := ltadd(100);
FreeLibrary(h);
ladd := nil;
ltadd := nil;
ShowMessage(i.ToString());
ShowMessage(k.ToString());
end;