April 28, 2014 logging | ||||
---|---|---|---|---|
| ||||
synchronized class EventLog{ void opCall(string s){ std.file.append("logfile.txt", s); } } shared EventLog eventLog; Pleas, is it multithread safe and prefered way? |
April 28, 2014 Re: logging | ||||
---|---|---|---|---|
| ||||
Posted in reply to xtimoner | 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 |
Copyright © 1999-2021 by the D Language Foundation