C# lock statement block: In a multithreading scenario how bad will be the performance hit?

Hi,
I have an existing code which apart from doing its main job writes to a log file. Now, the application is multithreaded and multiple threads can try to write to the log file. The existing code does not take care of thread synchronization. The basic structure of the code is -

public class A
{
     public void writeFullLogFile()
     {
          //call the function that actually writes to the log file
          B.writeToFile();
     }
}

public class B
{
     public static void writeToFile()
     {
          //actual file write happens here.
     }
}

All I am planning to do get the synchronization is to put a lock statement like below:

public class A
{
     private static Object lockMe = new Object();
     public void writeFullLogFile()
     {
          //call the function that actually writes to the log file
          lock(lockMe)
          {
               B.writeToFile();
          }
     }
}


Now, I want to know how badly does the lock statement affects the performance? How does the performance degrade with an increase in the number of concurrent threads?

Without changing my existing solution much is there a better way to have synchronization?
stinger_02inAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Gorkem YukselConnect With a Mentor Commented:
I would for all intense and purposes, create a STATIC LIST<String> object.. then Add() a log entry into this.. this way, the log file will have it's own log pool.. as you write each entry, delete it from the list. This way, you don't need to lock the file, but rather have it write at a time interval everything that is contained in the list.. if more entries are added to the list while writing, these will be written as it get to them.  The log will then be written on a first come/first served basis for each line.

If it doesn't make sense, please let me know.

Cheers,

G.
0
 
stinger_02inAuthor Commented:
Thanks for that suggestion. But I would really like to know what is the problem with a 'lock' statement? If I use it will the performance hit be really big?
0
 
Gorkem YukselConnect With a Mentor Commented:
With using the LOCK function, you run the possibility of blocking all the other threads.. Basically, it will then act more like a single threaded application, rather than multi-threaded.  If all the threads are waiting on each other to finish, it defeats the purpose of multi-threading.

With the option to have a timer set to write log files within a LIST array, it allows you to run your threads, have them complete and keep doing whatever else it needs to do.. the log message itself is queued to be written, so the thread doesn't have to sit and wait.

Cheers,

G.

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
mastooCommented:
Wouldn't you need to lock on the List<> anyway?
0
 
Gorkem YukselCommented:
nope.. because you will simply just Add() to the static list array.. no need to lock it.
0
 
stinger_02inAuthor Commented:
As a separate approach can we use TraceSource.Write from System .Diagnostics to write to the log file? As TraceSource is thread safe?
0
 
mastooConnect With a Mentor Commented:
I'll quibble on List<>.Add by quoting microsoft documentation: To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

And I'm a little confused as to why my documentation doesn't list a Write method on TraceSource.  But the other non-static methods are noted as not thread-safe.

Having said that I might venture your lock approach is usually fine.  It would kind of depend how much logging you do relative to the other processing.  If you don't want processing to ever wait on a write you use the cache with a lazy write as mentioned previously, or just do locking and an asynchronous write.
0
 
stinger_02inAuthor Commented:
Sorry for that...the method actually is TraceSource.TraceInformation. And this method is probably not static. So I suppose it is not thread safe either.

The implementation using static List<> seems to be the best.

One final question to get a better understanding: Consider two scenarios -
              a. Using locks to write the shared file
              b. Using TraceSource.TraceInformation() to do the same
Do they offer the same performance bottlenecks?
When one thread is writing to the shared file, will the rest of the threads get blocked and wait for the first thread to complete writing? If so, is it same for both scenarios a and b?

TIA,
0
 
mastooCommented:
Good question.  For the locks writing to a file the answer is yes.  But for TraceInformation it doesn't seem to be clearly documented.  A quick test would be put a 10 second delay in your listener and see how long TraceInformation takes.  If it blocks for 10 seconds then the two approaches would be equivalent.  If it doesn't block, then it seems the more attractive option for you.  I might guess it will block.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.