Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-05-14
9
Medium Priority
?
1,624 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 420 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 420 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 180 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

572 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