System.Threading.Monitor usage

V

Vladimir

Hi All,
I created a base class to write log files in threads.
Several classes inherited from my base class writing log files with
different file names.
Right now I lock a mutex equal to full file name to allow just one thread to
write.
What would be correct Monitor class usage in this scenario? What object has
to be locked to lock access only to a specific file instead of locking all?

Is that possible to have my base class to write different log files in
threads locking access just to a single file?

Thanks
Vladimir
 
P

Peter Duniho

Vladimir said:
Hi All,
I created a base class to write log files in threads.
Several classes inherited from my base class writing log files with
different file names.
Right now I lock a mutex equal to full file name to allow just one thread to
write.
What would be correct Monitor class usage in this scenario? What object has
to be locked to lock access only to a specific file instead of locking all?

Typically, each sub-class of your base class would be instantiated, and
in each instance you would have a private instance of System.Object used
as the synchronization object. Also typically, as a matter of
convenience one would just use the "lock" statement rather than the
Monitor class explicitly.
Is that possible to have my base class to write different log files in
threads locking access just to a single file?

Yes, of course. But according to your original description, you have
sub-classes that each have a specific log file to which they write.
Unless you are using a single instance of your "base" class to handle
all logging (in which case there are no sub-classes at all), there's no
need to introduce the complexity of having a single instance manage
multiple log files, synchronization or otherwise.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top