Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using standard pre-tested, pre-built stuff is always good (for example, Boost as another answer suggested), but this is something that's not too hard to build yourself. Here's a dumb little implementation pulled out from a project of mine:</p> <pre><code>#include &lt;pthread.h&gt; struct rwlock { pthread_mutex_t lock; pthread_cond_t read, write; unsigned readers, writers, read_waiters, write_waiters; }; void reader_lock(struct rwlock *self) { pthread_mutex_lock(&amp;self-&gt;lock); if (self-&gt;writers || self-&gt;write_waiters) { self-&gt;read_waiters++; do pthread_cond_wait(&amp;self-&gt;read, &amp;self-&gt;lock); while (self-&gt;writers || self-&gt;write_waiters); self-&gt;read_waiters--; } self-&gt;readers++; pthread_mutex_unlock(&amp;self-&gt;lock); } void reader_unlock(struct rwlock *self) { pthread_mutex_lock(&amp;self-&gt;lock); self-&gt;readers--; if (self-&gt;write_waiters) pthread_cond_signal(&amp;self-&gt;write); pthread_mutex_unlock(&amp;self-&gt;lock); } void writer_lock(struct rwlock *self) { pthread_mutex_lock(&amp;self-&gt;lock); if (self-&gt;readers || self-&gt;writers) { self-&gt;write_waiters++; do pthread_cond_wait(&amp;self-&gt;write, &amp;self-&gt;lock); while (self-&gt;readers || self-&gt;writers); self-&gt;write_waiters--; } self-&gt;writers = 1; pthread_mutex_unlock(&amp;self-&gt;lock); } void writer_unlock(struct rwlock *self) { pthread_mutex_lock(&amp;self-&gt;lock); self-&gt;writers = 0; if (self-&gt;write_waiters) pthread_cond_signal(&amp;self-&gt;write); else if (self-&gt;read_waiters) pthread_cond_broadcast(&amp;self-&gt;read); pthread_mutex_unlock(&amp;self-&gt;lock); } void rwlock_init(struct rwlock *self) { self-&gt;readers = self-&gt;writers = self-&gt;read_waiters = self-&gt;write_waiters = 0; pthread_mutex_init(&amp;self-&gt;lock, NULL); pthread_cond_init(&amp;self-&gt;read, NULL); pthread_cond_init(&amp;self-&gt;write, NULL); } </code></pre> <p><code>pthreads</code> not really being Windows-native, but the general idea is here. This implementation is slightly biased towards writers (a horde of writers can starve readers indefinitely); just modify <code>writer_unlock</code> if you'd rather the balance be the other way around.</p> <p>Yes, this is C and not C++. Translation is an exercise left to the reader.</p> <h2>Edit</h2> <p>Greg Rogers pointed out that the POSIX standard does specify <code>pthread_rwlock_*</code>. This doesn't help if you don't have <code>pthreads</code>, but it stirred my mind into remembering: <a href="http://sourceware.org/pthreads-win32/" rel="noreferrer">Pthreads-w32</a> should work! Instead of porting this code to non-<code>pthreads</code> for your own use, just use Pthreads-w32 on Windows, and native <code>pthreads</code> everywhere else.</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. 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.
 

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