I’m trying to implement a simple cross-process notify/observe system, using a pthread mutex and condition variable in shared (mapped) memory. It seems to be working fine (on macOS 11.6) if one process calls pthread_cond_wait and then another calls pthread_cond_broadcast — the waiting process indeed wakes up.
However, if two processes try to observe at the same time, the second one's call to pthread_cond_wait fails with EINVAL. I’m wondering if I’m just doing something wrong in my setup, or if this sort of usage isn’t supported.
Basically I create and mmap a file, initialize a pthread mutex and condition in the mapped memory using the setpshared attributes, then lock the mutex and notify or wait on the condition. Actual source code here:
Here’s the code that does the pthreads stuff
Here’s the outer code that opens and mmaps the file
I’m aware that there are a few dozen 🙄 Apple IPC APIs that are probably preferred over these POSIX ones. I’ve used some in the past. I’m doing it this way because:
(a) this is in a cross-platform project and it would be nice to share code between Unix platforms, at least Darwin and Linux;
(b) the thing I’m notifying/observing about is a database file, so tying the notifications to a side file next to the database provides ideal scoping;
(c) it’s similar in principle to the usage of shared memory for locking in SQLite and LMDB. (The difference is, I’m doing notification not locking.)
Any advice?
—Jens
Topic:
App & System Services
SubTopic:
Processes & Concurrency
Tags:
Concurrency
Inter-process communication