Solved

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

Posted on 2009-05-14
9
1,564 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

705 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

19 Experts available now in Live!

Get 1:1 Help Now