lock-free

Lock-free cache implementation in C++11

柔情痞子 提交于 2019-12-22 12:01:20
问题 Is there any way in C++11 to implement a lock-free cache for an object, which would be safe to access from multiple threads? The calculation I'm looking to cache isn't super cheap but also isn't super expensive, so requiring a lock would defeat the purpose of caching in my case. IIUC, std::atomic isn't guaranteed to be lock-free. Edit: Since calculate isn't -too- expensive, I actually don't mind if it runs once or twice too many. But I -do- need to make sure all consumers get the correct

Add the first element to a ConcurrentLinkedQueue atomically

余生长醉 提交于 2019-12-22 10:56:51
问题 I want to use a ConcurrentLinkedQueue in an atomic lock-free manner: Several concurrent threads push events into the queue and some other thread will process them. The queue is not bound and I don't want any thread to wait or get locked. The reading part however may notice that the queue got empty. In a lock free implementation the reading thread must not block but will just end its task and proceeds executing other tasks (i.e. as an ExecutorService). Thus the writer pushing the first new

Lockfree standard collections and tutorial or articles

时光怂恿深爱的人放手 提交于 2019-12-22 08:52:04
问题 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. 回答1: Check out Julian M Bucknall's blog. He describes (in detail) lock-free implementations of queues, lists, stacks, etc. http://www.boyet.com/Articles

Are lock-free atomics address-free in practice?

拟墨画扇 提交于 2019-12-22 04:49:11
问题 Boost.Interprocess is a wonderful library that simplifies the usage of shared memory amongst different processes. It provides mutexes, condition variables, and semaphores, which allow for synchronization when writing and reading from the shared memory. However, in some situations these (relatively) performance-intensive synchronization mechanisms are not necessary - atomic operations suffice for my use case, and will likely give much better performance. Unfortunately, Boost.Interprocess does

Adding blocking functions to lock-free queue

蓝咒 提交于 2019-12-21 20:29:07
问题 I have a lock-free multi producer, single consumer queue, based on a circular buffer. So far, it only has non-blocking push_back() and pop_front() calls. Now I want to add blocking versions of those calls, but I want to minimize the impact this has on the performance of code that uses the non-blocking versions - namely, it should not turn them into " lock-by-default " calls. E.g. the simplest version of a blocking push_back() would look like this: void push_back_Blocking(const T& pkg) { if (

lock free arena allocator implementation - correct?

廉价感情. 提交于 2019-12-21 17:33:34
问题 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 * Alloc(size_t size) { if (end-head < size) return 0; // allocation failure void * result = head; head += size; return head; } My attempt at a thread safe implementation

Lock free constructs in .net

回眸只為那壹抹淺笑 提交于 2019-12-21 16:57:49
问题 I am new to .net and would like to know whether .net has the java equivalent of AtomicInteger, ConcurrentLinkedQueue, etc? I did a bit of search and couldnt come up with anything. The lock free algorithms need some sort of a CAS instruction, which is provided through the undocumented Unsafe class in Java, does .net have anything equivalent? 回答1: In .NET there is the Interlocked class, with static methods Interlocked.Increment() and Interlocked.Decrement(). See http://msdn.microsoft.com/en-us

Is a lock required with a lazy initialization on a deeply immutable type?

北战南征 提交于 2019-12-21 03:58:08
问题 If I have a deeply immutable type (all members are readonly and if they are reference type members, then they also refer to objects that are deeply immutable). I would like to implement a lazy initialized property on the type, like this: private ReadOnlyCollection<SomeImmutableType> m_PropName = null; public ReadOnlyCollection<SomeImmutableType> PropName { get { if(null == m_PropName) { ReadOnlyCollection<SomeImmutableType> temp = /* do lazy init */; m_PropName = temp; } return m_PropName; }

Interlocked and Memory Barriers

你。 提交于 2019-12-20 09:58:30
问题 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 Interlocked.Exchange in Foo() guarantees that when Bar() is executed, I'll see the value "1"? (even if the value already exists in a register or cache line?) Or do I need to

Reproduce torn reads of decimal in c#

放肆的年华 提交于 2019-12-20 03:54:17
问题 Seeing is believing. Can anyone reproduce a program that reads a torn decimal? I tried spinning up multiple threads changing the same decimal between 1 and 2. I did not catch any reads different from 1 or 2. EDIT: I like to see that a reader thread does not see a atomic change from a writer thread, so the value should be something different from 1 or 2. void TornDecimalReadTest() { decimal sharedDecimal = 1; int threadCount = 100; var threads = new List<Thread>(); for (int i = 0; i <