#1 2014-06-09 08:14:55

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Is using single TSynLog object in multiple threads considered safe?

Hello Arnaud,

In a TThread object (TMainThread) I have a TSynLog object (not class):

TMainThread = class
  FLogObj: TSynLog;
  ...
end;

And when this TMainThread object is executed, it'll spawn 'sub-threads'.

My question is, is it considered to be safe if those 'sub-threads' also using the FLogObj object of their 'parent-threads'? For example:

  //We use OmniThreadLibrary in this example
  Parallel.ParallelTask.NumTasks(aTaskCount);
  myTasks.Execute(
    //The following anonymous method will be executed in sub-threads and will have multiple instances determined by the aTaskCount param.
    procedure (const task: IOmniTask)
    begin
      //note: here FLogObj is a field of the parent thread.
      FLogObj.Log(sllInfo, 'testing');
    end
  )

I read that TSynLog is thread-safe, but I'm not sure if the way I use is thread-safe or not, I appreciate your help!

Edit 1: Some basic tests shows that the above code runs OK without any access violation errors, but I have not done massive (stress) tests.

Last edited by edwinsn (2014-06-09 08:21:09)


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#2 2014-06-09 08:19:10

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: Is using single TSynLog object in multiple threads considered safe?

You can safely share the tsynlog instance among threads.

You can identify each thread in the log by the corresponding setting.

Offline

#3 2014-06-09 09:34:36

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: Is using single TSynLog object in multiple threads considered safe?

Thanks for the clarification, ab!


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

Board footer

Powered by FluxBB