Last active
April 3, 2020 07:18
-
-
Save nglee/b9dd4d8f55010d47a48f30f4967b86d1 to your computer and use it in GitHub Desktop.
Nonautomatic unlocking of interprocess_mutex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/interprocess/managed_xsi_shared_memory.hpp> | |
#include <boost/interprocess/xsi_key.hpp> | |
#include <iostream> | |
using namespace boost::interprocess; | |
void first_process(managed_xsi_shared_memory *shm); | |
void second_process(managed_xsi_shared_memory *shm); | |
int main() | |
{ | |
managed_xsi_shared_memory *shm = nullptr; | |
try { | |
shm = new managed_xsi_shared_memory{ create_only, xsi_key("bitest3.cpp", 239), 1024 }; | |
first_process(shm); | |
} catch (interprocess_exception& e) { | |
shm = new managed_xsi_shared_memory{ open_only, xsi_key("bitest3.cpp", 239) }; | |
second_process(shm); | |
} | |
} | |
void first_process(managed_xsi_shared_memory *shm) | |
{ | |
std::cout << "This is the first process." << std::endl; | |
interprocess_mutex *mtx = shm->find_or_construct<interprocess_mutex>("gMutex")(); | |
mtx->lock(); | |
std::cout << "The first process locked an interprocess_mutex" << std::endl; | |
std::cout << "and is going to exit without unlocking the mutex" << std::endl; | |
} | |
void second_process(managed_xsi_shared_memory *shm) | |
{ | |
std::cout << "This is the second process." << std::endl; | |
interprocess_mutex *mtx = shm->find_or_construct<interprocess_mutex>("gMutex")(); | |
std::cout << "The second process is going to lock an interprocess mutex," << std::endl; | |
std::cout << "and if it succeeds, then a message will be shown." << std::endl; | |
std::cout << "If it failes, it will run indefinitely." << std::endl; | |
mtx->lock(); | |
std::cout << "The second process locked the mutex!" << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment