English 中文(简体)
Delphi - 更新全球范围,从第二版
原标题:Delphi - Updating a global string from a second thread

我正在Delphi(XE)进行多读实验,并陷入了使用全球可变性的问题,这种可变性在主要的地球同步点和第二层工作线之间。

我的项目涉及通过一些档案扫描的第2个工人线,并更新了以现有档案名称显示的全球体。 当时,这一全球变化是通过主要世界森林日read子上的一个时间推来的,并更新了地位障碍。

我注意到,它偶尔会遇到一个因瓦利德点行动......或记忆外或工作刚刚停止反应(可能错锁)。

因此,我制作了一个测试仪,以查明并大大增加错误的可能性,这样就可以看到正在发生什么。

type
  TSyncThread = class(TThread)
  protected
    procedure Execute; override;
end;

var
  Form11: TForm11;
  ProgressString : String;
  ProgressCount : Int64;
  SyncThread : TSyncThread;
  CritSect : TRTLCriticalSection;

implementation

{$R *.dfm}

procedure TForm11.StartButtonClick(Sender: TObject);
begin
  Timer1.Enabled := true;
  SyncThread := TSyncThread.Create(True);
  SyncThread.Start;
end;

procedure TForm11.StopbuttonClick(Sender: TObject);
begin
  Timer1.Enabled := false;
  SyncThread.Terminate;
end;

procedure TForm11.Timer1Timer(Sender: TObject);
begin
  StatusBar1.Panels[0].Text :=  Count:   + IntToStr(ProgressCount);
  StatusBar1.Panels[1].Text := ProgressString;
end;

procedure TSyncThread.Execute;
var
  i : Int64;
begin
  i := 0;
  while not Terminated do begin
    inc(i);
    EnterCriticalSection(CritSect);
    ProgressString := IntToStr(i);
    ProgressCount := i;
    LeaveCriticalSection(CritSect);
  end;
end;

initialization
  InitializeCriticalSection(CritSect);
finalization
  DeleteCriticalSection(CritSect);

我将时间间隔定为10米,这样可以读到很多东西,而工人的read子正在平息地更新全球的var体。 在出现上述错误之前,仅剩下第二次。

我的问题是,《维也纳条约法公约》时间表中的“全球老鼠”的阅读操作是否有必要放在一个关键部分? - 如果是,为什么? 根据我的理解,这只是一个读物,而且已经用在关键部分的文字,即不能看它为什么会成为一个问题。 如果把读到时段也放在一个关键部分,那么它会做一些细微的工作。

I am new to multithreading so would appreciate any help in explaining why this simple example causes all sorts of problems and if there is a better way to be accessing a string from a worker thread.

最佳回答

Delphi String is allocated on a heap, it is not a static buffer somewhere. The variable itself is just a pointer. When your reading thread accesses a String, and at the same time this very string is being deallocated by another thread, bad things happen. You are accessing already freed memory, possibly allocated again for something else, etc.

Even if this String was a static buffer, update operations are not atomic, therefore you could be using a corrupted string that is being updated at this very moment (half new data and half old).

So you need to protect your reading operations with the same critical section you used around the writing operations.

问题回答

暂无回答




相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签