Note that there are some explanatory texts on larger screens.

plurals
  1. POthread synchronization: making sure function gets called in order
    primarykey
    data
    text
    <p>I'm writing a program in which I need to make sure a particular function is called is not being executed in more than one thread at a time.</p> <p>Here I've written some simplified pseudocode that does exactly what is done in my real program.</p> <pre><code>mutex _enqueue_mutex; mutex _action_mutex; queue _queue; bool _executing_queue; // called in multiple threads, possibly simultaneously do_action() { _enqueue_mutex.lock() object o; _queue.enqueue(o); _enqueue_mutex.unlock(); execute_queue(); } execute_queue() { if (!executing_queue) { _executing_queue = true; enqueue_mutex.lock(); bool is_empty = _queue.isEmpty(); _enqueue_mutex.lock(); while (!is_empty) { _action_mutex.lock(); _enqueue_mutex.lock(); object o = _queue.dequeue(); is_empty = _queue.isEmpty(); _enqueue_mutex.unlock(); // callback is called when "o" is done being used by "do_stuff_to_object_with_callback" also, this function doesn't block, it is executed on its own thread (hence the need for the callback to know when it's done) do_stuff_to_object_with_callback(o, &amp;some_callback); } _executing_queue = false; } } some_callback() { _action_mutex.unlock(); } </code></pre> <p>Essentially, the idea is that <code>_action_mutex</code> is locked in the while loop (I should say that <code>lock</code> is assumed to be blocking until it can be locked again), and expected to be unlocked when the completion callback is called (<code>some_callback</code> in the above code).</p> <p>This, does not seem to be working though. What happens is if the <code>do_action</code> is called more than once at the same time, the program locks up. I think it might be related to the while loop executing more than once simultaneously, but I just cant see how that could be the case. Is there something wrong with my approach? Is there a better approach?</p> <p>Thanks</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. 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