all groups > dotnet clr > june 2007 >
You're in the

dotnet clr

group:

lock-free programming


lock-free programming Shawn B.
6/27/2007 9:22:33 AM
dotnet clr: Greetings,

Why is lock-free programming more efficient than using monitors or mutex?
What is it that makes lock-free programming more efficient? I'm having a
hard time comprehending this.


Thanks,
Shawn

Re: lock-free programming Gabriele G. Ponti
6/27/2007 1:07:29 PM
http://en.wikipedia.org/wiki/Non-blocking_synchronization

[quoted text, click to view]

Re: lock-free programming Günter Prossliner
6/27/2007 7:12:23 PM
Hello Shawn!

[quoted text, click to view]

There are mainly two factors involved here:

1. When you use locking, threads of your application will wait to acquire
the lock instead of doing something usefull

2. There is an overhead involved when you use the various synronization
objects. Of cause the use of the Interlocked... Functions (which are needed
for lock-free programming), also have some overhead, but less than the
traditional syncronization objects.

But remember: Producing code for multithreaded environments is not easy.
Producing lock-free code is even much more complicated and time-consuming.
So in my opinion lock-free coding should not replace traditional
syncronization methods. It shall be used in VERY critical methods / classes
where performance absolutly nessersary, and locking has been identified as
the bottleneck.



:: GP

Re: lock-free programming Jon Skeet [C# MVP]
6/27/2007 7:26:41 PM
[quoted text, click to view]

1) Lock-free code never has to wait for another thread to finish before
continuing
2) The very act of acquiring and releasing a lock is "costly" (not
very, but it's not free either)

I personally don't attempt lock-free programming in general - I'll very
occasionally use a volatile variable or Interlocked.Increment, but
that's about it. For anything else I use locks.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: lock-free programming Ben Voigt [C++ MVP]
6/29/2007 1:58:22 AM

[quoted text, click to view]

If you organized lock-based programming the same way as lock-free, you would
see almost as big a performance improvement. However, locks are often too
broad -- taken too often and held too long. Lock-free programming always
prepares a result, unsynchronized, then does a test-and-set to commit the
result if the input didn't change.

[quoted text, click to view]

That's not precisely true. There's almost always a loop on test-and-set,
and you can't leave that loop until you get access to the shared resource.
The time for potential collision and retry is very slim though.

[quoted text, click to view]
Re: lock-free programming Ben Voigt [C++ MVP]
6/29/2007 2:02:12 AM

[quoted text, click to view]

Sure it should -- as well-tested reusable code written by an expert. SList
is an example of this. Lock-free containers can be just as easy to use as a
simple container in single threaded code, but with the added thread safety
for a low cost.

[quoted text, click to view]

Actually, locking is harder to get right than using a prebuilt thread-safe
object, which is what lock-free programming usually boils down to. In any
case, the amount of actual volatile code should be kept very small to ease
reasoning about the system. And due to the lower cost of lock-free, it's
reasonable to use it a lot more extensively than locks.
Re: lock-free programming Ben Voigt [C++ MVP]
6/29/2007 2:05:03 AM

[quoted text, click to view]

Simply put, the locking primitive can't be implemented with a lock. (More
advanced locks can be created with simpler locks). So every lock has at
least two operations implemented with lock-free threadsafe programming --
acquiring ownership of the lock, and releasing the lock. Right there, it's
already twice as expensive as a lock-free operation.
Re: lock-free programming Jon Skeet [C# MVP]
6/29/2007 5:38:07 PM
[quoted text, click to view]

Yes - you often see people who are reasonably new to threading who
think that by taking locks out all the time you end up with thread-safe
code. In fact you end up with deadlocks, and when you don't have
deadlocks you have poor performance :(

Not only does reducing lock spans obviously reduce the amount of
waiting for locks to become available, but I believe that taking an
uncontested lock takes significantly more cycles than taking a lock
which is initially contested.

[quoted text, click to view]

Yes, true enough.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
AddThis Social Bookmark Button