#include #include #include #include #include #include #include #include #include namespace bi = boost::interprocess; using SharedMutex = bi::named_sharable_mutex; using ReadLock = bi::sharable_lock; using WriteLock = bi::scoped_lock; using NewEntryCondition = bi::named_condition_any; constexpr char SHM_NAME[] = "shared_mem"; constexpr char MUT_NAME[] = "shm_mut"; constexpr char COND_NAME[] = "shm_cond"; constexpr char CUR_INT_NAME[] = "shm_int"; int main(int argc, char *argv[]) { // Open the shared mem, condition variable, mutex bi::managed_shared_memory segment(bi::open_only, SHM_NAME); SharedMutex sh_mut(bi::open_only, MUT_NAME); NewEntryCondition sh_cond(bi::open_only, COND_NAME); int& shared_int = *segment.find("shared_int").first; int copy_of_int = -1; for (int i=0;;i++) { { ReadLock r_lock( sh_mut ); std::cout << "calling named_condition_any::wait()" << std::endl; sh_cond.wait( r_lock, [&shared_int, ©_of_int]() { std::cout << "checking predicate..." << std::endl; return (copy_of_int < shared_int); }); copy_of_int = shared_int; std::cout << "copy of int = " << copy_of_int << std::endl; } } return 0; }