Home > Back-end >  Multithreaded scenarios, Delphi callback function results agree why not
Multithreaded scenarios, Delphi callback function results agree why not

Time:09-21

A c + + DLL which need to pass in a callback function of Delphi, returns a string as a result, but in the case of multi-threaded concurrent, the inside of the callback function return value will not be consistent, is this why? Trouble to solve the train of thought,
c + + DLL code:
#include
#include
# define WIN32_LEAN_AND_MEAN//Exclude rarely - informs the stuff from Windows headers
//the Windows Header Files:
#include
#include

Typedef void (CALLBACK * DoDisptchCmd) (const char * name, char * & amp; The callback);


DoDisptchCmd disptcher=NULL;

BOOL APIENTRY DllMain (HMODULE HMODULE, dwords ul_reason_for_call LPVOID lpReserved)
{
Return TRUE;
}

Unsigned __stdcall SecondThreadFunc (void * pArguments)
{
Char * callback=NULL;
Disptcher (" 1 ", the callback);
Printf (" CALL1=======Address: % d, Content: % s \ n ", (int) & amp; The callback, the callback);
If (STRCMP (callback, "nihao")!=0)
Printf (" ERROR CALL1=======Address: % d, Content: % s \ n ", (int) & amp; The callback, the callback);
return 0;
}

Unsigned __stdcall SecondThreadFunc1 (void * pArguments)
{
Char * callback=NULL;
Disptcher (" 2 ", the callback);
Printf (" CALL2=======Address: % d, Content: % s \ n ", (int) & amp; The callback, the callback);
If (STRCMP (callback, "nihao2")!=0)
Printf (" ERROR CALL2=======Address: % d, Content: % s \ n ", (int) & amp; The callback, the callback);
return 0;
}

Unsigned __stdcall SecondThreadFunc2 (void * pArguments)
{
Char * callback=NULL;
Disptcher (" 3 ", the callback);
Printf (" CALL3=======Address: % d, Content: % s \ n ", (int) & amp; The callback, the callback);
If (STRCMP (callback, "nihao3")!=0)
Printf (" ERROR CALL3=======Address: % d, Content: % s \ n ", (int) & amp; The callback, the callback);
return 0;
}

Void WINAPI Start ()
{
While (true)
{
HANDLE hThread;
Unsigned threadID;
HThread=(HANDLE) _beginthreadex (NULL, 0, & amp; SecondThreadFunc, NULL, 0, & amp; ThreadID);
HANDLE hThread1;
Unsigned threadID1;
HThread1=(HANDLE) _beginthreadex (NULL, 0, & amp; SecondThreadFunc1, NULL, 0, & amp; ThreadID1);
HANDLE hThread2;
Unsigned threadID2;
HThread2=(HANDLE) _beginthreadex (NULL, 0, & amp; SecondThreadFunc2, NULL, 0, & amp; ThreadID2);
Sleep (10);
}
}


Void WINAPI Register (DoDisptchCmd callback)
{
Disptcher=callback;
}

DELPHI code:
The program Project1.

{$APPTYPE CONSOLE}

USES the
SysUtils;

Type
TDODISPTCHCMDCB=procedure (const name: PAnsiChar; Var Callback: PAnsiChar); Stdcall;

The function TestAdd (I, J: Integer) : HRESULT; Stdcall;
External 'Test. DLL' name 'TestAdd';
Procedure RegisterCallback (callback: TDODISPTCHCMDCB); Stdcall;
External 'Test. DLL' name 'Register'.
Procedure Start (); Stdcall;
External 'Test. DLL' name 'Start';

Procedure CallbackFunction (url: PAnsiChar; Var callback: PAnsiChar); Stdcall;
Var
Content: the string;
The begin
If (url='1') then
Content:='nihao'
Else if (url='2') then
Content:='nihao2'
Else if (url='3') then
Content:='nihao3';

The callback:=PAnsiChar (AnsiString (Content));
end;


The begin
Try
{TODO oUser - cConsole Main: Insert code here}
RegisterCallback (@ CallbackFunction);
Start ().
Except,
On E: the Exception do
Writeln (e.c. with our fabrication: lassName, ':', E.M essage);
end;
End.

result:
CALL1=======Address: 38666040, the Content: nihao
CALL2=======Address: 39714616, the Content: nihao2
The ERROR CALL2=======Address: 39714616, the Content: nihao3
CALL3=======Address: 40763192, the Content: nihao3
The ERROR CALL1=======Address: 38666040, the Content: nihao3
CALL1=======Address: 38666040, the Content: nihao
  • Related