Link to home
Start Free TrialLog in
Avatar of werehamster-
werehamster-

asked on

1 MDI Form, 2 Indy Reader Threads, 1 INIFile. How do I make it thread safe?

Greetings,

This question spawns off of from another question...
https://www.experts-exchange.com/questions/21136526/Properly-Coded-Indy-Reader-Threads-Part-3.html

Right now I have an Indy Reader Thread, that spawns a 2nd Indy Reader Thread to a 2nd Login Server.  Both threads need information from an INI file.  Right now, the INI file is a property of the form which uses the INI also.  I have tried using a critical section around all the reads to it, but it still does not work properly.

Here is a link to the source that I am using...
http://www.datazap.net/ftp/werehamster/MyBot/3.zip

Please, I need example source of how to use a single INI file between multible threads and a form.  This is a critical part of my program and is only used during login so performance isn't an issue.
Avatar of Jacco
Jacco
Flag of Netherlands image

It would be a good decision to separate the TWerebot from TBNLSReader and TBNETReader. It is always dangerous to have them in one unit since they can then mess with each other private parts <G> to easily.

At this moment the use of the INI file is threadsafe because you only use it in the FormCreate of TWereBot if you want to start using it from one of the threads as well it might be a problem.

You could make a threadsafe wrapper around the INI:

unit Unit1;

interface

uses
  IniFiles, SysUtils;

type
  TSafeIniFile = class(TIniFile)
  private
    fGuard: TMultiReadExclusiveWriteSynchronizer;
  public
    constructor Create(const FileName: string);
    destructor Destroy; override;
    function ReadString(const Section, Ident, Default: string): string; override;
    procedure WriteString(const Section, Ident, Value: string); override;
  end;

implementation

{ TSafeIniFile }

constructor TSafeIniFile.Create(const FileName: string);
begin
  inherited Create(FileName);
  fGuard := TMultiReadExclusiveWriteSynchronizer.Create;
end;

destructor TSafeIniFile.Destroy;
begin
  fGuard.Free;
  inherited Destroy;
end;

function TSafeIniFile.ReadString(const Section, Ident, Default: string): string;
begin
  fGuard.BeginRead;
  try
    Result := inherited ReadString(Section, Ident, Default);
  finally
    fGuard.EndRead;
  end;
end;

procedure TSafeIniFile.WriteString(const Section, Ident, Value: string);
begin
  fGuard.BeginWrite;
  try
    inherited WriteString(Section, Ident, Value);
  finally
    fGuard.EndWrite;
  end;
end;

end.

You should also protect:

    procedure ReadSection(const Section: string; Strings: TStrings); override;
    procedure ReadSections(Strings: TStrings); override;
    procedure ReadSectionValues(const Section: string; Strings: TStrings); override;

inside BeginRead/EndRead's and

    procedure EraseSection(const Section: string); override;
    procedure DeleteKey(const Section, Ident: String); override;
    procedure UpdateFile; override;

inside BeginWrite/EndWrites

Now all threads including the forms main thread can share in instance of the INIFile

Regards Jacco

Avatar of werehamster-
werehamster-

ASKER

Hmm, never used this before...

TMultiReadExclusiveWriteSynchronizer;

I assume it is better than TCriticalSection?

So basically I treat is as .enter and .leave, but for file access stuff?
ASKER CERTIFIED SOLUTION
Avatar of Jacco
Jacco
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think I gave a correct solution.
Shrug, did it myself.  :)