lock-free

Memory barriers in userspace? (Linux, x86-64)

It is easy to set memory barriers on the kernel side: the macros mb, wmb, rmb, etc. are always in place thanks to the Linux kernel headers. How to accomplish this on the user side? ...

Is this code thread-safe?

This is a simplified version of some code I'm currently maintaining: int SomeFunc() { const long lIndex = m_lCurrentIndex; int nSum = 0; nSum += m_someArray[lIndex]; nSum += m_someArray[lIndex]; return nSum; } lCurrentIndex is updated periodically by another thread. The question is; will making a local copy of m_CurrentInd...

What is the difference between lockless and non-blocking?

Hi, In the context of data-structures synchronization, can someone clarify the difference between "lockless" and "non-blocking"? These terms seem to be used interchangeably by a lot of people but I'm not yet sure if there isn't some subtle difference hidden somewhere. I mean lockless is "without locks" and non-blocking is more like gua...

Internal implementation of atomic_cas_64() on Solaris on Sparc?

On 64-bit Solaris on Sparc, is the atomic_cas_64() function call implemented using load-link/condition-store? If not, what if any API does Solaris offer for user-mode C code to utilize ll/sc? ...

Does SPARC v9 have a double word compare and swap instruction?

So; on a 64 bit SPARC CPU which is v9 compliant, there exists I know a cas instruction. This operates on single word length values. I've also seen on the web reference to a casx instruction - but I can't find out anything much more about it. I'm wondering - is this a double word compare and swap? And if not, the general question is; ...

lock free arena allocator implementation - correct?

for a simple pointer-increment allocator (do they have an official name?) I am looking for a lock-free algorithm. It seems trivial, but I'd like to get soem feedback whether my implementaiton is correct. not threadsafe implementation: byte * head; // current head of remaining buffer byte * end; // end of remaining buffer void * All...

Why is lockless concurrency such a big deal (in Clojure)?

I'm told that Clojure has lockless concurrency and that this is Important. I've used a number of languages but didn't realize they were performing locks behind the scenes. Why is this an advantage in Clojure (or in any language that has this feature)? ...

Is lock free multithreaded programming making anything easier?

I only read a little bit about this topic, but it seems that the only benefit is to get around contention problems but it will not have any important effect on the deadlock problem as the code which is lock free is so small and fundamental (fifos, lifos, hash) that there was never a deadlock problem. So it's all about performance - is t...

Is the C# "lock" construct rendered obselete by Interlocked.CompareExchange<T>?

Summary: It seems to me that: wrapping fields representing a logical state into a single immutable consumable object updating the object's authoritative reference with a call to Interlocked.CompareExchange<T> and handling update failures appropriately provides a kind of concurrency that renders the "lock" construct not only unnecess...

Do atomic operations work the same across processes as they do across threads?

Obviously, atomic operations make sure that different threads don't clobber a value. But is this still true across processes, when using shared memory? Even if the processes happen to be scheduled by the OS to run on different cores? Or across different distinct CPUs? Edit: Also, if it's not safe, is it not safe even on an operating sys...

When are lock free data structures less performant than mutual exclusion (mutexes)?

I read somewhere (can't find the page anymore) that lock free data structures are more efficient "for certain workloads" which seems to imply that sometimes they're actually slower or the gain from them can be zero in some situations. Taking the ~100 cycle hit of a lock instruction to do an atomic op sounds plenty faster to me than going...

Do atomic operations become slower as more CPUs are added?

x86 and other architectures provide special atomic instructions (lock, cmpxchg, etc.) that allow you to write 'lock free' data structures. But as more and more cores are added, it seems as though the work these instructions will actually have to do behind the scenes will grow (at least to maintain cache coherency?). If an atomic add take...

Thread-safe, lock-free increment function?

UPDATED: Is there a thread-safe, lock-free and available on all Linux distros increment function available in C or C++ ? ...

Lockfree standard collections and tutorial or articles.

Does someone know of a good resource for the implementation (meaning source code) of lock-free usual data types. I'm thinking of Lists, Queues and so on? Locking implementations are extremely easy to find but I can't find examples of lock free algorithms and how to exactly does CAS work and how to use it to implement those structures. ...

Using memory barriers

In the following code sample, does the memory barrier in FuncA is required to ensure that the most up-to-date value is read? class Foo { DateTime m_bar; void FuncA() // invoked by thread X { Thread.MemoryBarrier(); // is required? Console.WriteLine(m_bar); } void FuncB() // invoked by thread Y { m_...

When do writes/reads affect main memory?

When I write a value into a field, what guarantees do I get regarding when the new value will be saved in the main memory? For example, how do I know that the processor don't keep the new value in it's private cache, but updated the main memory? Another example: int m_foo; void Read() // executed by thread X (on processor #0) { Co...

How does memory fences affect "freshness" of data?

I have a question about the following code sample (taken from: http://www.albahari.com/threading/part4.aspx#_NonBlockingSynch) class Foo { int _answer; bool _complete; void A() { _answer = 123; Thread.MemoryBarrier(); // Barrier 1 _complete = true; Thread.MemoryBarrier(); // Barrier 2 } ...

How to programmatically tell if two variables are on the same stack? (in Windows)

I'm in a thread. I have an address. Is that address from a variable on the same stack that I'm using? static int *address; void A() { int x; atomic::CAS(address, 0, &x); // ie address = &x // ... } void B() { int y; int * addr = atomic::read(address); // ie addr = address if (addr && on_same_stack(&y, addr)) ...

Interlocked and Memory Barriers

I have a question about the following code sample (*m_value* isn't volatile, and every thread runs on a separate processor) void Foo() // executed by thread #1, BEFORE Bar() is executed { Interlocked.Exchange(ref m_value, 1); } bool Bar() // executed by thread #2, AFTER Foo() is executed { return m_value == 1; } Does using Inte...

Lock Free Deque that supports removing an arbitrary node

This needs to be lock free as it has to run in the interrupt handler of an SMP system. I cannot take locks. I have a contiguous array holding some values. Some of the entries in this array are "free", they are not occupied. I want to make a list of these entries so that I can quickly allocate one. However, I occasionally have to allocat...