Solved

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

Posted on 2009-05-14
9
1,588 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 21

Expert Comment

by:mastoo
ID: 24389189
Wouldn't you need to lock on the List<> anyway?
0
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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