digitalmars.D.learn - logging
- xtimoner (7/7) Apr 28 2014 synchronized class EventLog{
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (16/23) Apr 28 2014 Only if there is only one EventLog object. A synchronized class means
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
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