in reply to Re^6: OT: Locking and syncing mechanisms available on *nix.
in thread OT: Locking and syncing mechanisms available on *nix.

The idea's basically the same, though it's of course uglier using libpthread in C. You have to have both a flag to keep track of readiness, and a condition variable to queue up waiting threads. Here's a rough sketch:
#include <pthread.h> pthread_mutex_t lock; pthread_cond_t cond; int ready; void consumer() { pthread_mutex_lock(&lock); while (!ready) pthread_cond_wait(&cond, &lock); /* lock is locked, ready is true */ ready = 0; pthread_mutex_unlock(&lock); } void producer() { /* lock is unlocked */ pthread_mutex_lock(&lock); ready = 1; pthread_cond_signal(&cond, &lock); /* lock is still locked */ pthread_mutex_unlock(&lock); }
libpthread is POSIX, so it should be available anywhere.

Replies are listed 'Best First'.
Re^8: OT: Locking and syncing mechanisms available on *nix.
by BrowserUk (Patriarch) on Mar 27, 2011 at 16:11 UTC

    Thanks, but the trouble with that is we now need 3 elements. The flag, the cond_var and the mutex.

    And the only 'reason' for using the mutex appears to be to serialise access to the flag. But mutexes always require a user/kernel/user space transition which is horribly expensive, when the same job can be done on modern processors using memory barriers and atomic instructions.

    For example, the ready flag state changes can be safely done entirely in user space using gcc's Atomic builtins.

    But that still doesn't allow us to ditch the mutex, because the only signalled wait construct is the cond_wait, which requires a bloody mutex.

    Many of the issues, and solution to them, are discussed in Mutex .v. futex. What I've yet been unable to find is the documentation (and discussion of availablity) of futexes?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      It looks like there are futexes down there in the hairball that is glibc. AFAICT either they or atomic test-and-set are used to implement pthread mutexes, rather than an OS call (but I just glanced at the code, so don't take my word for it).

      If thread-switching performance is really critical, and pthreads are too slow, would some form of cooperative threading work?

        Thanks. I'll take a look at glibc. Any feel for how widely available it is across *nixes?

        If thread-switching performance is really critical, and pthreads are too slow,

        You've got the wrong end of the stick. It isn't the pthreads or threads that are too slow, it is threads::shared.

        Vis. Using Thread::Queue (which uses threads::shared and its cond vars and mutexes), the best throughput I've achieved between two threads is about 50,000 scalars/second.

        My current implementation is achieving (on Windows using event objects) 500,000 scalars/second reliably, and I've seen close to twice that.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.