

Using the std::atomic from the ‹atomic› library effectively removes the need to ever again being tempted to use discouraged platform dependent hacks. C++11 with its memory model, is available both on Linux and Windows.

In the old article I gave a promise to publish a C++11 empowered queue that is lock-free through the use of std::atomic.
Circular studio windows update#
It could break for something as simple as a compiler update or using it on another hardware architecture.

It breaks easily as it is basically an ugly platform hack that looks appealing through an illusion of simplicity. The old article described a type of thread communication that goes against good C++ practice using a circular FIFO queue that is highly platform dependent. The old article was written to try to understand this clearly discouraged technique but without recommending it. If reading the original version of this article (also published here!) there will be a description and reasoning of the how, why and when a hazardous technique could possibly work and when it would not work. The queue was used for an interrupt handler as it pushed incoming messages from the interrupt handling to the real-time system. In the military avionic industry I had seen what was in my view a hazardous technique for creating a lock-free circular FIFO. Background: Article Rationaleīack in 2009 I published my first CodeProject article about a lock-free circular FIFO queue. Issues that deals with this, such as overwrite of old items, multiple priority queues, or other ways of handling this scenario are outside the scope of this article.
Circular studio windows full#
Its maximum size and how that relates to the negative impact of dealing with a full queue must be carefully considered. Obviously a full queue for a Producer is a non-ideal situation. If there is no room left in the queue for the Producer it is not required to wait until there is space. If there are no jobs to process the Consumer thread can continue with other tasks. At the queue there is no waiting involved for adding or retrieving data. The Consumer can remove tasks from the queue as long as the queue is not empty. The Producer can add new tasks to the queue as long as the queue is not full. Using a FIFO queue gives predictable behaviour for the asynchronous delegation and processing of tasks as they pass from the Producer to the Consumer. The solution: Delegate time consuming jobs to another thread that receives (i.e. Examples could be : Interrupt/Signal handler, GUI events, data recorder, etc. The problem description: A high priority task that must not be delayed in lengthy operations and also requires the data processing to finish in the same order as they are started. This scenario is often referred to as the single producer → single consumer problem. This circular FIFO queue can be used for communicating between maximum two threads.

The power of wait-free and lock-free together makes this type of circular queue attractive in a range of areas, from interrupt and signal handlers to real-time systems or other time sensitive software. The lock-free nature of the queue enables two thread communication from a single source thread (the Producer) to a single destination thread (the Consumer) without using any locks. The wait-free nature of the queue gives a fixed number of steps for each operation. The wait-free and lock-free circular queue is a useful technique for time and memory sensitive systems.
