Hello lazarus-list,

I'm working in a program that needs multiple threads and one will
synchronize with main thread to display a log. I do not wish to
continue the caller thread meanwhile the Synchronize has not been
finished, so I had added a set of events to handle this. My program
locks up just after start, and the offended method is in the
Synchronize feature. So I start to rewrite the same idea around the
multithread example code keeping only the things that I need, and to
ask to you if this is a Synchronize bug/problem or a non-wanted
programming technique, and I must replace the code with a different
approach (if possible).

The main problem is here, in the execute method:

-----------------------------------------
procedure TMyThread.Execute;
begin
  fStatusText := 'TMyThread Starting...';
  Synchronize(@Showstatus);
  fStatusText := 'TMyThread Running...';
  while (not Terminated) and (true {any condition required}) do begin
    m_Event.WaitFor(1000); //It should be INFINITE...
    m_Event.ResetEvent;
    if NewStatus <> fStatusText then begin
      m_Lock.Enter;
      fStatusText := newStatus;
      Synchronize(@Showstatus);
      m_Lock.Leave;
    end;
    m_EventDone.SetEvent;
  end;
end;
-----------------------------------------

The idea is to modify "NewStatus" from an outside procedure
(CriticalSection protected) and activate m_Event to let the Execute
know that it must launch the syncronize, then it will wait for
"m_EventDone" to be signaled before continue.

Attached is the Multithread example mainunit.pas modified with my
changes to raise the bug/problem, simply replace the example file with
this one.

System: WinXP SP2
Lazarus: 0.9.24 and 0.9.25 #13510 (packages from the web)
FPC: As shipped with lazarus 2.2.0 and 2.2.1
CPU: Pentium Hyperthreading (tested without hyper too).

-- 
Best regards,
 JoshyFun
{
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 *                                                                         *
 ***************************************************************************

  Abstract:
    Demo to show, how to start a thread and how synchronize with the main
    thread.
    Important: The cthread unint must be added to the uses section of the .lpr
               file. See multithreadingexample1.lpr.
}
unit MainUnit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, 
ExtCtrls,syncobjs;

type

  { TMyThread }

  TMyThread = class(TThread)
  private
    fStatusText: string;
    procedure ShowStatus;
  protected
    m_Event: TEvent;
    m_EventDone: TEvent;
    m_Lock: TCriticalSection;
    procedure Execute; override;
  public
    newStatus : string;
    procedure SetString(S: String);
    constructor Create(CreateSuspended: boolean);
  end;

  { TForm1 }

  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
  public
    MyThread : TMyThread;
  end;

var
  Form1: TForm1; 

implementation

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyThread := TMyThread.Create(true); // With the True parameter it doesn't 
start automatically
  if Assigned(MyThread.FatalException) then
    raise MyThread.FatalException;
      
  // Here the code initialises anything required before the threads starts 
executing

  MyThread.Resume;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
//    newStatus:='TMyThread Time: '+FormatDateTime('YYYY-MM-DD HH:NN:SS',Now);

  MyThread.SetString('TMyThread Time: '+FormatDateTime('YYYY-MM-DD 
HH:NN:SS',Now));
end;

{ TMyThread }

procedure TMyThread.ShowStatus;
// this method is only called by Synchronize(@ShowStatus) and therefore
// executed by the main thread
// The main thread can access GUI elements, for example Form1.Caption.
begin
  Form1.Caption := fStatusText;
end;

procedure TMyThread.Execute;
begin
  fStatusText := 'TMyThread Starting...';
  Synchronize(@Showstatus);
  fStatusText := 'TMyThread Running...';
  while (not Terminated) and (true {any condition required}) do begin

    //here goes the code of the main thread loop
    m_Event.WaitFor(1000);
    m_Event.ResetEvent;
    if NewStatus <> fStatusText then begin
      m_Lock.Enter;
      fStatusText := newStatus;
      Synchronize(@Showstatus);
      m_Lock.Leave;
    end;
    m_EventDone.SetEvent;
  end;
end;

procedure TMyThread.SetString(S: String);
begin
  m_Lock.Enter;
  newStatus:=S;
  m_Lock.Leave;
  m_Event.SetEvent;
  //Wait for thread synchronize completation...
  m_EventDone.WaitFor(5000);
end;

constructor TMyThread.Create(CreateSuspended: boolean);
begin
  FreeOnTerminate := True;
  m_Event:=TEvent.Create(nil,true,false,'');
  m_EventDone:=TEvent.Create(nil,true,false,'');
  m_Lock:=TCriticalSection.Create;
  inherited Create(CreateSuspended);
end;

initialization
  {$I mainunit.lrs}

end.
_______________________________________________
Lazarus mailing list
[email protected]
http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply via email to