Solved

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

Posted on 2009-05-14
9
1,574 Views
Last Modified: 2012-05-07
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?
0
Comment
Question by:stinger_02in
  • 3
  • 3
  • 3
9 Comments
 
LVL 9

Accepted Solution

by:
Gorkem Yuksel earned 140 total points
ID: 24387559
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
 

Author Comment

by:stinger_02in
ID: 24387988
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
 
LVL 9

Assisted Solution

by:Gorkem Yuksel
Gorkem Yuksel earned 140 total points
ID: 24388971
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
 
LVL 21

Expert Comment

by:mastoo
ID: 24389189
Wouldn't you need to lock on the List<> anyway?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:Gorkem Yuksel
ID: 24392081
nope.. because you will simply just Add() to the static list array.. no need to lock it.
0
 

Author Comment

by:stinger_02in
ID: 24392314
As a separate approach can we use TraceSource.Write from System .Diagnostics to write to the log file? As TraceSource is thread safe?
0
 
LVL 21

Assisted Solution

by:mastoo
mastoo earned 60 total points
ID: 24398645
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
 

Author Comment

by:stinger_02in
ID: 24398924
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
 
LVL 21

Expert Comment

by:mastoo
ID: 24399099
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now