Channels ▼
RSS

C/C++

Lock-free Interprocess Communication


Algorithm One

This algorithm demonstrates the main idea. Consider the simplest case, which involves two processes (or threads; for the purposes of this article, I'll use "processes" and "threads" interchageably). I'll call them Writer and Reader. Writer is transmitting one non-zero processor word of data to Reader and gets confirmation that data has been delivered successfully. This is done by writing this word and confirmation into shared memory.

In our case, it is important that shared memory which is used to transmit data between processes is aligned by the size of the processor word. Aligning guarantees that read/write operations to the main memory will be atomic. Atomic in this case is assumed to mean that if a value of a word initially was V1, and a process writes another value V2, then any other process or thread even without any synchronisation will read either the old value V1 or the new value V2.

The algorithm itself is the following:

  1. Initially the word in the shared memory has zero value.
  2. When Writer needs to transmit a non-zero processor word to Reader, it writes this word into shared memory.
  3. Reader continuously reads a processor word from the shared memory and compares it with zero. When it reads a non-zero value it means that data has arrived.
  4. Reader writes back to the same place in the shared memory a zero value to confirm that data has arrived.
  5. Writer reads shared memory words until it reads zero. This means that Writer has got confirmation from Reader that data was transmitted successfully.

This algorithm does not guarantee how fast data will be delivered from one process to another, or how fast confirmation will be delivered back. This is similar to sending a network packet from one computer to another and receiving a confirmation.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video