Note that there are some explanatory texts on larger screens.

plurals
  1. POSTL and multithread
    primarykey
    data
    text
    <p>I'm aware, that I need to use mutex, when I perform operations on single STL container inside multiple threads. However I want to know if there are any exceptions from this rule. Please consider simplified scenario I'm trying to implement.</p> <p>I have multiple threads adding elements to container and operation is surrounded with mutex lock/unlock. Then threads notify somehow (e.g. using eventfd on linux) single thread dedicated to dispatch elements in this container. What I want to do is to access first element in container without using mutex. Sample code based on deque but note that I ca use any container with queue capability:</p> <pre><code>std::mutex locker; std:deque&lt;int&gt; int_queue; int fd; // eventfd eventfd_t buffer; bool some_condition; </code></pre> <p>Thread 1, 2, 3, etc.</p> <pre><code>locker.lock (); int_queue.push_back (1); locker.unlock (); eventfd_write (fd, 1); </code></pre> <p>Thread dedicated to dispatch elements:</p> <pre><code>while (true) { bool some_condition (true); locker.lock (); if (int_quque.empty () == false) { locker.unlock (); } else { locker.unlock (); eventfd_read (fd, &amp;buffer); } while (some_condition) { int&amp; data (int_queue.front ()); some_condition = some_operation (data); // [1] } locker.lock (); int_queue.pop (); locker.unlock (); } </code></pre> <p>[1] I will do some_operation() on signle element many times, that's why I want to avoid mutex locking here. It's to expensive.</p> <p>I want to know if this code can lead to any synchronisation problems or something.</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.
 

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