Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Comment (not answer):</strong></p> <p>Is that what you have in mind?</p> <p>Global:</p> <pre> // protected by m: pthread_mutex_t m; pthread_cond_t c; bool about_to_pthread_cond_wait = false; bool condition_waited_on = false; </pre> <p>Thread A :</p> <pre> pthread_mutex_lock (&m); { // locked region about_to_pthread_cond_wait = true; while (condition_waited_on) { // pthread_cond_wait (&m, &c) is decomposed here: __pthread_mutex_cond_wait_then_unlock (&m, &c); // unlocked region pthread_mutex_lock (&m); } } pthread_mutex_unlock (&m); </pre> <p>Thread B:</p> <pre> for (bool break_loop = false; !break_loop;) { pthread_mutex_lock (&m); { // locked region condition_waited_on = true; if (about_to_pthread_cond_wait) { pthread_cond_signal (&c); pthread_cond_destroy (&c); break_loop = true; } } pthread_mutex_unlock (&m); pthread_yield (); } </pre> <p>EDIT:</p> <p>I assume <code>pthread_cond_wait (&amp;m, &amp;c);</code> does:</p> <pre> __pthread_mutex_cond_wait_then_unlock (&m, &c); pthread_mutex_lock (&m); </pre> <p><code>__pthread_mutex_cond_wait_then_unlock</code> will being monitoring the signals to the CV, then unlock the mutex, then go to sleep.</p> <p>If the CV has an internal mutex that <code>__pthread_mutex_cond_wait_then_unlock</code> and <code>pthread_cond_signal</code> must lock internaly, then I assume <code>__pthread_mutex_cond_wait_then_unlock</code> does:</p> <pre> pthread_mutex_lock (&c.int_mutex); { // locked region for c.int_state __register_wakeup_cond (&c.int_state); pthread_mutex_unlock (&m); } pthread_mutex_unlock (&c.int_mutex); // will not touch c.int_state any more __sleep_until_registered_wakeups (); </pre> <p>and <code>pthread_cond_signal</code> does:</p> <pre> pthread_mutex_lock (&c.int_mutex); { // locked region for CV internal state __wakeup_registered (&c.int_state); } pthread_mutex_unlock (&c.int_mutex); </pre> <p>Then <code>pthread_cond_destroy</code> will only be called after <code>__pthread_mutex_cond_wait_then_unlock</code> has finished using <code>c.int_state</code>.</p>
 

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