Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of the Java object that you would use to wait/notify, you need two objects: a mutex and a condition variable. These are initialized with <code>pthread_mutex_init</code> and <code>pthread_cond_init</code>.</p> <p>Where you would have synchronized on the Java object, use <code>pthread_mutex_lock</code> and <code>pthread_mutex_unlock</code> (note that in C you have to pair these yourself manually). If you don't need to wait/notify, just lock/unlock, then you don't need the condition variable, just the mutex. Bear in mind that mutexes are not necessarily "recursive", This means that if you're already holding the lock, you can't take it again unless you set the init flag to say you want that behaviour.</p> <p>Where you would have called <code>java.lang.Object.wait</code>, call <code>pthread_cond_wait</code> or <code>pthread_cond_timedwait</code>.</p> <p>Where you would have called <code>java.lang.Object.notify</code>, call <code>pthread_cond_signal</code>.</p> <p>Where you would have called <code>java.lang.Object.notifyAll</code>, call <code>pthread_cond_broadcast</code>.</p> <p>As in Java, spurious wakeups are possible from the wait functions, so you need some condition which is set before the call to signal, and checked after the call to wait, and you need to call <code>pthread_cond_wait</code> in a loop. As in Java, the mutex is released while you're waiting.</p> <p>Unlike Java, where you can't call <code>notify</code> unless you hold the monitor, you <em>can</em> actually call <code>pthread_cond_signal</code> without holding the mutex. It normally doesn't gain you anything, though, and is often a really bad idea (because normally you want to lock - set condition - signal - unlock). So it's best just to ignore it and treat it like Java.</p> <p>There's not really much more to it, the basic pattern is the same as Java, and not by coincidence. Do read the documentation for all those functions, though, because there are various flags and funny behaviours that you want to know about and/or avoid.</p> <p>In C++ you can do a bit better than just using the pthreads API. You should at least apply RAII to the mutex lock/unlock, but depending what C++ libraries you can use, you might be better off using a more C++-ish wrapper for the pthreads functions.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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