www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - logging

reply "xtimoner" <xtimoner gmail.com> writes:
synchronized class EventLog{
     void opCall(string s){
         std.file.append("logfile.txt", s);
     }
}

shared EventLog eventLog;

Pleas, is it multithread safe and prefered way?
Apr 28 2014
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/28/2014 01:16 PM, xtimoner wrote:

 synchronized class EventLog{
      void opCall(string s){
          std.file.append("logfile.txt", s);
      }
 }

 shared EventLog eventLog;

 Pleas, is it multithread safe and prefered way?
Only if there is only one EventLog object. A synchronized class means that there can be only one member function executed on the same object at a given time. Your code is the equivalent of the following: class EventLog{ void opCall(string s){ synchronized (this) { // <-- NOTE (this) std.file.append("logfile.txt", s); } } } So, opCall (and other member functions) is synchronized on a particular object. If you have more than one EventLog, then they can write to the same file at the same time and that would be a problem. Ali
Apr 28 2014