Showing tag results for Code

Apr 21, 2011
Post comments count0
Post likes count1

The performance improvements of a lock-free algorithm is often not in the locking

Raymond Chen
Raymond Chen

GWO wonders what the conditions are under which the lock-free version significantly outpeforms a simple critical section. Remember that switching to a lock-free algorithm should be guided by performance measurements. Switching from a simple algorithm to a complex one shouldn't be done unless you know that the simple algorithm is having trouble....

Code
Apr 20, 2011
Post comments count0
Post likes count1

Corrections to Patterns for using the InitOnce functions

Raymond Chen
Raymond Chen

Adam Rosenfield pointed out that it is not possible to fail an asynchronous initialization; if you pass when completing an asynchronous initialization, the function fails with . (Serves me right for writing an article the night before it goes up.) A more correct version is therefore In other words, the pattern is as follows: While I'm her...

Code
Apr 19, 2011
Post comments count0
Post likes count1

Visual Studio 2005 gives you acquire and release semantics for free on volatile memory access

Raymond Chen
Raymond Chen

If you are using Visual Studio 2005 or later, then you don't need the weird function because Visual Studio 2005 and later automatically impose acquire semantics on reads from volatile locations. It also imposes release semantics on writes to volatile locations. In other words, you can replace the old function with the following: This is a g...

Code
Apr 18, 2011
Post comments count0
Post likes count1

Don't forget to include the message queue in your lock hierarchy

Raymond Chen
Raymond Chen

In addition to the loader lock, the message queue is another resource that people often forget to incorporate into their lock hierarchy. If your code runs on a UI thread, then it implicitly owns the message queue whenever it is running, because messages cannot be dispatched to a thread until it calls a message-retrieval function such as or . In o...

Code
Apr 15, 2011
Post comments count0
Post likes count1

Lock-free algorithms: The try/commit/(hand off) model

Raymond Chen
Raymond Chen

The last lock-free pattern for this week isn't actually lock-free, but it does run without blocking. The pattern for what I'll call try/commit/(hand off) is more complicated than the other patterns, so I'll start off by describing it in words rather than in code, because the code tends to make things more complicated. First, you take the state...

Code
Apr 14, 2011
Post comments count0
Post likes count1

Lock-free algorithms: The opportunistic cache

Raymond Chen
Raymond Chen

Suppose profiling reveals that a specific calculation is responsible for a significant portion of your CPU time, and instrumentation says that most of the time, it's just being asked to calculate the same thing over and over. A simple one-level cache would do the trick here. Of course, this isn't thread-safe, because if one thread is pre-empted...

Code
Apr 13, 2011
Post comments count0
Post likes count1

Lock-free algorithms: Update if you can I'm feeling down

Raymond Chen
Raymond Chen

A customer was looking for advice on this synchronization problem: We have a small amount of data that we need to share among multiple processes. One way to protect the data is to use a spin lock. However, that has potential for deadlock if the process which holds the spinlock doesn't get a chance to release it. For example, it might be suspend...

Code
Apr 12, 2011
Post comments count0
Post likes count1

Lock-free algorithms: The try/commit/(try again) pattern

Raymond Chen
Raymond Chen

The singleton constructor pattern and the example we saw some time ago are really special cases of the more general pattern which I'll call try/commit/(try again). I don't know if this pattern has a real name, but that's what I'm calling it for today. The general form of this pattern goes like this: We calculate the desired new value based ...

Code
Apr 8, 2011
Post comments count0
Post likes count1

Lock-free algorithms: The singleton constructor (answer to exercises)

Raymond Chen
Raymond Chen

A few days ago, I asked you to make an existing class multithread-safe. The class caches objects called which are indexed by a 32-bit ID. The cache is implemented as an array that dynamically resizes as more items are added to it. A naïve multithreaded version might use a slim reader-writer lock with shared access on reads, exclusive access...

Code