Showing tag results for 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
Apr 7, 2011
Post comments count0
Post likes count1

Lock-free algorithms: The one-time initialization

Raymond Chen
Raymond Chen

A special case of the singleton constructor is simply lazy-initializing a bunch of variables. In a single-threaded application you can do something like this: This works fine in a single-threaded program, but if the program is multi-threaded, then two threads might end up trying to lazy-initialize the variables, and there are race conditions w...

Code
Apr 6, 2011
Post comments count0
Post likes count1

Lock-free algorithms: Choosing a unique value (solutions)

Raymond Chen
Raymond Chen

Last time, I left a warm-up exercise consisting of a code fragment which tries to compute a unique process-wide value. Here it is again: It may be easier to enumerate what the function does right rather than what it does wrong. Um, the words are correctly-spelled. That's about it. Damien was the first to note that the author basically...

Code
Apr 6, 2011
Post comments count0
Post likes count1

Lock-free algorithms: The singleton constructor

Raymond Chen
Raymond Chen

The first half may be familiar to many (most?) readers, but there's an interesting exercise at the bottom. A very useful pattern for the Interlocked* functions is lock-free lazy initialization via . Yes, that's a really long function name, but it turns out every part of it important. This is a double-check lock, but without the locking. Inste...

Code
Apr 5, 2011
Post comments count0
Post likes count1

Lock-free algorithms: Choosing a unique value (warm-up)

Raymond Chen
Raymond Chen

Here's a snippet of code whose job is to generate a unique number within the process. Here's some reference reading to get yourself in the mood. Caution: It may or may not be useful. Criticize this code fragment.

Code