logging
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Apr 28 14:09:24 PDT 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
More information about the Digitalmars-d-learn
mailing list