Note that there are some explanatory texts on larger screens.

plurals
  1. POboost::thread_group - is it ok to call create_thread after join_all?
    primarykey
    data
    text
    <p>I have the following situation:</p> <p>I create a boost::thread_group instance, then create threads for parallel-processing on some data, then join_all on the threads.</p> <p>Initially I created the threads for every X elements of data, like so:</p> <pre><code>// begin = someVector.begin(); // end = someVector.end(); // batchDispatcher = boost::function&lt;void(It, It)&gt;(...); boost::thread_group processors; // create dispatching thread every ASYNCH_PROCESSING_THRESHOLD notifications while(end - begin &gt; ASYNCH_PROCESSING_THRESHOLD) { NotifItr split = begin + ASYNCH_PROCESSING_THRESHOLD; processors.create_thread(boost::bind(batchDispatcher, begin, split)); begin = split; } // create dispatching thread for the remainder if(begin &lt; end) { processors.create_thread(boost::bind(batchDispatcher, begin, end)); } // wait for parallel processing to finish processors.join_all(); </code></pre> <p>but I have a problem with this: When I have lots of data, this code is generating lots of threads (> 40 threads) which keeps the processor busy with thread-switching contexts.</p> <p>My question is this: Is it possible to call create_thread on the thread_group <em>after</em> the call to join_all.</p> <p>That is, can I change my code to this?</p> <pre><code>boost::thread_group processors; size_t processorThreads = 0; // NEW CODE // create dispatching thread every ASYNCH_PROCESSING_THRESHOLD notifications while(end - begin &gt; ASYNCH_PROCESSING_THRESHOLD) { NotifItr split = begin + ASYNCH_PROCESSING_THRESHOLD; processors.create_thread(boost::bind(batchDispatcher, begin, split)); begin = split; if(++processorThreads &gt;= MAX_ASYNCH_PROCESSORS) // NEW CODE { // NEW CODE processors.join_all(); // NEW CODE processorThreads = 0; // NEW CODE } // NEW CODE } // ... </code></pre> <p>Whoever has experience with this, thanks for any insight.</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.
 

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