#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[]) { // Remove the shared mem, condition variable, mutex struct shm_remove { shm_remove() { bi::shared_memory_object::remove( SHM_NAME ); } ~shm_remove() { bi::shared_memory_object::remove( SHM_NAME ); } } remover; struct mut_remove { mut_remove() { SharedMutex::remove(MUT_NAME); } ~mut_remove() { SharedMutex::remove(MUT_NAME); } } mut_remover; struct cond_remove { cond_remove() { NewEntryCondition::remove(COND_NAME); } ~cond_remove() { NewEntryCondition::remove(COND_NAME); } } cond_remover; // Create the shared mem, condition variable, mutex bi::managed_shared_memory segment(bi::create_only, SHM_NAME, 2*65536); SharedMutex sh_mut(bi::create_only, MUT_NAME); NewEntryCondition sh_cond(bi::create_only, COND_NAME); int& shared_int = *segment.construct("shared_int")(0); for (int i=0;;i++) { std::this_thread::sleep_for(std::chrono::seconds(2)); { WriteLock w_lock( sh_mut ); shared_int = i; std::cout << "set shared_int to: " << shared_int << std::endl; sh_cond.notify_all(); } } return 0; }