C
CADVector
Unregistered / Unconfirmed
GUEST, unregistred user!
我做了以下程序,通过100个线程同时对一个文件进行读写,为什么会出问题?
动态库如下:
unit ExportUt;
interface
uses Math, SysUtils, SyncObjs, Classes;
procedure InitDebug(ACircleFileCount, AFileSize: Integer;
ADebugFilePath, ADebugFileName: pChar);stdcall;
procedure WriteDebug(ADebugStr: string);stdcall;
procedure FreeDebug;stdcall;
procedure ChangeFileStream;stdcall;
exports
InitDebug,
WriteDebug,
FreeDebug;
var
_CircleFileCount: Integer;
_DebugFileName: string;
_DebugFilePath: string;
_CurrentFileId: Integer;
_Lock: TCriticalSection;
_FileStream: TFileStream;
_FileSize: Integer;
implementation
procedure ChangeFileStream;
begin
Inc(_CurrentFileID);
if _CurrentFileId > _CircleFileCount then
_CurrentFileId := 1;
if Assigned(_FileStream) then
FreeAndNil(_FileStream);
_FileStream := TFileStream.Create(_DebugFilePath + _DebugFileName + IntToStr(_CurrentFileId)+'.log', fmCreate);
end;
procedure InitDebug(ACircleFileCount, AFileSize: Integer;
ADebugFilePath, ADebugFileName: pChar);
begin
_Lock := TCriticalSection.Create;
_CircleFileCount := Max(1,ACircleFileCount);
_DebugFileName := ADebugFileName;
_DebugFilePath := ADebugFilePath;
_CurrentFileId := 1;
_FileSize := AFileSize;
ForceDirectories(_DebugFilePath);
end;
procedure WriteDebug(ADebugStr: string);
var
DebugStr: string;
begin
try
_Lock.Enter;
//检查当前文件是否存在
if not FileExists(_DebugFilePath+_DebugFileName+IntToStr(_CurrentFileId)+'.log') then
begin
_FileStream := TFileStream.Create(_DebugFilePath+_DebugFileName+IntToStr(_CurrentFileId)+'.log', fmCreate);
end;
if not Assigned(_FileStream) then
_FileStream := TFileStream.Create(_DebugFilePath+_DebugFileName+IntToStr(_CurrentFileId)+'.log', fmOpenWrite);
_FileStream.Seek(0, soFromEnd );
DebugStr := DateTimeToStr(now) + '-->' + ADebugStr+ #13#10;
_FileStream.Write(DebugStr[1], Length(DebugStr)) ;
//检查文件是否过大
if _FileStream.Size > _FileSize*1024 then
begin
ChangeFileStream;
end;
finally
_Lock.Leave;
end;
end;
procedure FreeDebug;
begin
_Lock.Free;
end;
end.
动态库如下:
unit ExportUt;
interface
uses Math, SysUtils, SyncObjs, Classes;
procedure InitDebug(ACircleFileCount, AFileSize: Integer;
ADebugFilePath, ADebugFileName: pChar);stdcall;
procedure WriteDebug(ADebugStr: string);stdcall;
procedure FreeDebug;stdcall;
procedure ChangeFileStream;stdcall;
exports
InitDebug,
WriteDebug,
FreeDebug;
var
_CircleFileCount: Integer;
_DebugFileName: string;
_DebugFilePath: string;
_CurrentFileId: Integer;
_Lock: TCriticalSection;
_FileStream: TFileStream;
_FileSize: Integer;
implementation
procedure ChangeFileStream;
begin
Inc(_CurrentFileID);
if _CurrentFileId > _CircleFileCount then
_CurrentFileId := 1;
if Assigned(_FileStream) then
FreeAndNil(_FileStream);
_FileStream := TFileStream.Create(_DebugFilePath + _DebugFileName + IntToStr(_CurrentFileId)+'.log', fmCreate);
end;
procedure InitDebug(ACircleFileCount, AFileSize: Integer;
ADebugFilePath, ADebugFileName: pChar);
begin
_Lock := TCriticalSection.Create;
_CircleFileCount := Max(1,ACircleFileCount);
_DebugFileName := ADebugFileName;
_DebugFilePath := ADebugFilePath;
_CurrentFileId := 1;
_FileSize := AFileSize;
ForceDirectories(_DebugFilePath);
end;
procedure WriteDebug(ADebugStr: string);
var
DebugStr: string;
begin
try
_Lock.Enter;
//检查当前文件是否存在
if not FileExists(_DebugFilePath+_DebugFileName+IntToStr(_CurrentFileId)+'.log') then
begin
_FileStream := TFileStream.Create(_DebugFilePath+_DebugFileName+IntToStr(_CurrentFileId)+'.log', fmCreate);
end;
if not Assigned(_FileStream) then
_FileStream := TFileStream.Create(_DebugFilePath+_DebugFileName+IntToStr(_CurrentFileId)+'.log', fmOpenWrite);
_FileStream.Seek(0, soFromEnd );
DebugStr := DateTimeToStr(now) + '-->' + ADebugStr+ #13#10;
_FileStream.Write(DebugStr[1], Length(DebugStr)) ;
//检查文件是否过大
if _FileStream.Size > _FileSize*1024 then
begin
ChangeFileStream;
end;
finally
_Lock.Leave;
end;
end;
procedure FreeDebug;
begin
_Lock.Free;
end;
end.