How expensive is the lock statement?
How expensive is the lock statement?
I've been experimenting with multi threading and parallel processing and I needed a counter to do some basic counting and statistic analysis of the speed of the processing. To avoid problems with concurrent use of my class I've used a lock statement on a private variable in my class:
private object mutex = new object(); public void Count(int amount) { lock(mutex) { done += amount; } }
But I was wondering... how expensive is locking a variable? What are the negative effects on performance?
Answer by Jake Pearson for How expensive is the lock statement?
Here is an article that goes into the cost. Short answer is 50ns.
Answer by Adam Maras for How expensive is the lock statement?
This doesn't answer your query about performance, but I can say that the .NET Framework does offer an Interlocked.Add
method that will allow you to add your amount
to your done
member without manually locking on another object.
Answer by Henk Holterman for How expensive is the lock statement?
lock
(Monitor.Enter/Exit) is very cheap, cheaper than alternatives like a Waithandle or Mutex.
But what if it was (a little) slow, would you rather have a fast program with incorrect results?
Answer by KeithS for How expensive is the lock statement?
There are a few different ways to define "cost". There is the actual overhead of obtaining and releasing the lock; as Jake writes, that's negligible unless this operation is performed millions of times.
Of more relevance is the effect this has on the flow of execution. This code can only be entered by one thread at a time. If you have 5 threads performing this operation on a regular basis, 4 of them will end up waiting for the lock to be released, and then to be the first thread scheduled to enter that piece of code after that lock is released. So, your algorithm is going to suffer significantly. How much so depends on the algorithm and how often the operation is called.. You can't really avoid it without introducing race conditions, but you can ameliorate it by minimizing the number of calls to the locked code.
Answer by Hans Passant for How expensive is the lock statement?
The technical answer is that this is impossible to measure or quantify, it heavily depends on the state of the CPU memory write-back buffers. Which is very non-deterministic.
The practical answer is that it is waaaay cheaper than the amount of time you'll burn on debugging your code when you think you can skip a lock.
Answer by Johan Nilsson for How expensive is the lock statement?
The cost for a lock in a tight loop, compared to an alternative with no lock, is huge. You can afford to loop many times and still be more efficient than a lock. That is why lock free queues are so efficient.
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LockPerformanceConsoleApplication { class Program { static void Main(string[] args) { var stopwatch = new Stopwatch(); const int LoopCount = (int) (100 * 1e6); int counter = 0; for (int repetition = 0; repetition < 5; repetition++) { stopwatch.Reset(); stopwatch.Start(); for (int i = 0; i < LoopCount; i++) lock (stopwatch) counter = i; stopwatch.Stop(); Console.WriteLine("With lock: {0}", stopwatch.ElapsedMilliseconds); stopwatch.Reset(); stopwatch.Start(); for (int i = 0; i < LoopCount; i++) counter = i; stopwatch.Stop(); Console.WriteLine("Without lock: {0}", stopwatch.ElapsedMilliseconds); } Console.ReadKey(); } } }
Output:
With lock: 2013 Without lock: 211 With lock: 2002 Without lock: 210 With lock: 1989 Without lock: 210 With lock: 1987 Without lock: 207 With lock: 1988 Without lock: 208
Answer by ipavlu for How expensive is the lock statement?
Oh dear!
It seems that correct answer flagged here as THE ANSWER is inherently incorrect! I would like to ask the author of the answer, respectfully, to read the linked article to the end. article
The author of the article from 2003 article was measuring on Dual Core machine only and in the first measuring case, he measured locking with single thread only and result was about 50ns per lock access.
It says nothing about a lock in the concurrent environment. So we have to continue reading the article and in the second half the author was measuring locking scenario with two and three threads, which gets closer to concurrency levels of today's processors.
So the author says, that with two threads on Dual Core, the locks costs 120ns, and with 3 threads it goes to 180ns. So it seems to be clearly dependent on number of concurrently accessed threads and more is worse.
So it is simple, it is not 50 ns, unless it is a single thread, where the lock gets useless.
Another issue for consideration is that it is measured as average time!
If the time of iterations would be measured, there would be even times between 1ms to 20ms, simple because the majority was fast, but few threads will be waiting for processors time and incur even milliseconds long delays.
This is bad news for any kind of application which requires high throughput, low latency.
And the last issue for consideration is that there could be slower operations inside the lock and very often that is the case. The longer the block of code is executed inside the lock, the higher the contention is and delays rise sky high.
Please consider, that over one decade has passed already from 2003, that is few generations of processors designed specifically to run fully concurrently and locking is considerably harming theirs performance.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment