我正在编写一个Delphi dll,它计算并将结果写入CSV文件。调用程序是多线程的,因此一个问题是同时多次写入文件会导致调用程序崩溃。我试图使用临界区来锁定文件写入,但仍然会发生崩溃。如果我将程序配置为仅使用一个线程,则问题将消失。以下是我的代码:
library Question;
uses
SysUtils,
Classes,
Math,
SyncObjs;
{$R *.res}
Var
Outputfile: textfile;
CriticalSection: TCriticalSection;
CalNumb: integer = 0;
PrintString: String;
Threadvar
Cal1, Cal2, Cal1Last: double;
Function Calculator (input1, input2, input3, input4: double;
Factor: double; LastCal: Boolean; Print: integer): double stdcall;
Const
Divisor = 4;
Var
Temp: double;
Begin
Cal1Last:= Cal1;
Cal1:= (input1+ input2+input3+ input4)/Divisor;
Cal2:= (Cal1+Factor*Cal1Last)/2;
Temp:= Cal2 - Cal1Last;
If LastCal and (Print = 1) then
begin
CriticalSection:= TCriticalSection.Create;
Try
Try
Inc(CalNumb);
Assign(Outputfile, 'C:\Calculator\Result.csv');
If FileExists('C:\Calculator\Result.csv') then
Append(Outputfile) else rewrite (Outputfile);
If CalNumb = 1 then
begin
PrintString:= 'CalNumb' + ',' + 'Cal1' + ',' +
'Cal1Last' + ',' + 'Cal2' + ',';
Writeln(Outputfile, PrintString);
end;
Writeln(Outputfile,
CalNumb, ',', Cal1:5:2, ',', Cal1Last:5:2, ',', Cal2:5:2, ',');
Finally
Close(Outputfile);
End;
Finally
CriticalSection.Free;
End;
end;
If Cal1 <> 0 then Calculator:= Temp/Cal1 else Calculator:= 0;
End;
Exports
Calculator;
begin
end.
我的代码中是否有错误?多线程计算和外部文件编写对于此项目至关重要。你有什么建议和意见吗?我是初学者,所以如果你能在这里发布你的代码,对我来说将是一个很大的帮助。非常感谢你提前! ////////////////////////////////////////////////// ////////////////////////////////////////////////
Graymatter,谢谢你的建议。在我建议您进行更改后,应用程序会在写入文件之前崩溃。之前版本的dll可以在崩溃之前将某些数据行写入文件。如果我可能做出错误的更改,我会在下面发布更改的部分。代码的另一部分没有改变。
If LastCal and (Print = 1) then
begin
CriticalSection.Acquire;
Try
Try
Inc(CalNumb);
Assign(Outputfile, 'C:\Calculator\Result.csv');
If FileExists('C:\Calculator\Result.csv') then
Append(Outputfile) else rewrite (Outputfile);
If CalNumb = 1 then
begin
PrintString:= 'CalNumb' + ',' + 'Cal1' + ',' +
'Cal1Last' + ',' + 'Cal2' + ',';
Writeln(Outputfile, PrintString);
end;
Writeln(Outputfile,
CalNumb, ',', Cal1:5:2, ',', Cal1Last:5:2, ',', Cal2:5:2, ',');
Finally
Close(Outputfile);
End;
Finally
CriticalSection.Release;
End;
end;
If Cal1 <> 0 then Calculator:= Temp/Cal1 else Calculator:= 0;
End;
Exports
Calculator;
begin
end.
initialization
CriticalSection := TCriticalSection.Create;
finalization
CriticalSection.Free;
end;
答案 0 :(得分:6)
创建关键部分对象不会进行任何锁定。您需要致电Acquire
以获取锁定,并Release
释放锁定。
请参阅在线帮助中的Using Critical Sections。
问题是在加载DLL时需要创建关键部分,因此您需要执行以下操作:
begin
...
CriticalSection.Acquire;
try
...
// The code that needs to be locked goes inside here.
// In your case it would be the code that opens the file
// and appends the data.
...
finally
CriticalSection.Release;
end;
...
end;
您必须将代码移到单独的单元中,以便包含initialization
和finalization
块。这些块将在首次加载DLL时以及何时卸载时执行。
您的新单元将如下所示:
unit UnitForDLL;
interface
function Calculator(input1, input2, input3, input4: double;
Factor: double; LastCal: Boolean; Print: integer): double; stdcall;
implementation
uses
SyncObjs, SysUtils;
var
...
CriticalSection: TCriticalSection;
...
function Calculator(input1, input2, input3, input4: double;
Factor: double; LastCal: Boolean; Print: integer): double; stdcall;
begin
...
CriticalSection.Acquire;
try
...
// The code that needs to be locked goes inside here.
// In your case it would be the code that opens the file
// and appends the data.
...
finally
CriticalSection.Release;
end;
...
end;
initialization
CriticalSection := TCriticalSection.Create;
finalization
CriticalSection.Free;
end.
完成后,您的项目本身将变得更加简单:
library Question;
uses
SysUtils,
Classes,
UnitForDLL;
{$R *.res}
exports
Calculator;
begin
end.