Note that there are some explanatory texts on larger screens.

plurals
  1. POwhat is the fastest way to notify another thread that data is available? any alternativies to spinning?
    primarykey
    data
    text
    <p>One my thread writes data to circular-buffer and another thread need to process this data ASAP. I was thinking to write such simple <code>spin</code>. Pseudo-code!</p> <pre><code> while (true) { while (!a[i]) { /* do nothing - just keep checking over and over */ } // process b[i] i++; if (i &gt;= MAX_LENGTH) { i = 0; } } </code></pre> <p>Above I'm using <code>a</code> to indicate that data stored in <code>b</code> is available for processing. Probaly I should also set thread afinity for such "hot" process. Of course such spin is very expensive in terms of CPU but it's OK for me as my primary requirement is <strong>latency</strong>.</p> <p>The question is - am I should really write something like that or <code>boost</code> or <code>stl</code> allows something that:</p> <ol> <li>Easier to use.</li> <li>Has roughly the same (or even better?) <strong>latency</strong> at the same time occupying less CPU resources?</li> </ol> <p>I think that my pattern is so general that there should be some good implementation somewhere.</p> <p><strong>upd</strong> It seems my question is still too complicated. Let's just consider the case when i need to write some items to array in arbitrary order and another thread should read them in right order as items are available, how to do that?</p> <p><strong>upd2</strong></p> <p>I'm adding test program to demonstrate what and how I want to achive. At least on my machine it happens to work. I'm using <code>rand</code> to show you that I can not use general <code>queue</code> and I need to use <code>array-based</code> structure:</p> <pre><code>#include "stdafx.h" #include &lt;string&gt; #include &lt;boost/thread.hpp&gt; #include "windows.h" // for Sleep const int BUFFER_LENGTH = 10; int buffer[BUFFER_LENGTH]; short flags[BUFFER_LENGTH]; void ProcessorThread() { for (int i = 0; i &lt; BUFFER_LENGTH; i++) { while (flags[i] == 0); printf("item %i received, value = %i\n", i, buffer[i]); } } int _tmain(int argc, _TCHAR* argv[]) { memset(flags, 0, sizeof(flags)); boost::thread processor = boost::thread(&amp;ProcessorThread); for (int i = 0; i &lt; BUFFER_LENGTH * 10; i++) { int x = rand() % BUFFER_LENGTH; buffer[x] = x; flags[x] = 1; Sleep(100); } processor.join(); return 0; } </code></pre> <p>Output:</p> <pre><code>item 0 received, value = 0 item 1 received, value = 1 item 2 received, value = 2 item 3 received, value = 3 item 4 received, value = 4 item 5 received, value = 5 item 6 received, value = 6 item 7 received, value = 7 item 8 received, value = 8 item 9 received, value = 9 </code></pre> <p>Is my program guaranteed to work? How would you redesign it, probably using some of existent structures from boost/stl instead of array? Is it possible to get rid of "spin" without affecting latency?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload