Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If your algorithm has separate passes that fill the queue or consume the queue, consider using tbb::concurrent_vector. It has a push_back method that could be used for the fill pass, and an at() method for the consumption passes. If threads contend to pop elements in a consumption pass, consider using a tbb::atomic counter to generate indices for at().</p> <p>If there is no such clean separation of filling and consuming, using at() would probably create more problems than it solves, even if it existed, because it would be racing against a consumer.</p> <p>If a consumption pass just needs to loop over the concurrent_vector in parallel, consider using tbb::parallel_for for the loop. tbb::concurrent_vector has a range() method that supports this idiom.</p> <pre><code>void consume( tbb::concurrent_vector&lt;T&gt;&amp; vec ) { tbb::parallel_for( vec.range(), [&amp;]( const tbb::concurrent_vector&lt;T&gt;::range_type&amp; r ) { for( auto i=r.begin(); i!=r.end(); ++i ) { T value = *i; ...process value...; } }); } </code></pre> <p>If a consumption pass cannnot use tbb:parallel_for, consider using a TBB atomic counter to generate the indices. Initialize the counter to zero and use ++ to increment it. Here is an example: </p> <pre><code>tbb::atomic&lt;size_t&gt; head; tbb::concurrent_vector&lt;T&gt; vec; bool pop_one( T&amp; result ) { // Try to grab next item from vec size_t i = head++; // Fetch-and-increment must be single atomic operation if( i&lt;vec.size() ) { result = vec[i]; return true; } else { return false; // Failed } } </code></pre> <p>In general, this solution will be less scalable than using tbb::parallel_for, because the counter "head" introduces a point of contention in the memory system.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      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