A C++17 library for single-instance applications
set(guardpp_tests OFF)
If set to
ON
, guardpp will build a test executable.
- FetchContent
include(FetchContent) FetchContent_Declare(lockpp GIT_REPOSITORY "https://github.com/Soundux/guardpp") FetchContent_MakeAvailable(guardpp) target_link_libraries(<YourLibrary> guardpp)
- Git Submodule
git submodule add "https://github.com/Soundux/guardpp"
# Somewhere in your CMakeLists.txt add_subdirectory("<path_to_guardpp>") target_link_libraries(<YourLibrary> guardpp)
#include <guard.hpp>
#include <iostream>
int main()
{
guardpp::guard instance_guard("guardpp");
auto other_instance = instance_guard.other_instance();
if (other_instance)
{
if (other_instance.value())
{
std::cout << "Another instance is running!" << std::endl;
}
else
{
std::cout << "No other instance is running!" << std::endl;
}
}
else
{
std::cout << other_instance.error() << std::endl;
}
// You can also take-over an existing lock by calling instance_guard.reset();
}
expected
tiny-process-library
(For tests only!)
-
guard::reset()
is unimplemented on Windows. This is because on Windows a mutex can only be deleted by closing all existing handles to the mutex, we can not do this because we can't access the handles created by other processes.ยน -
The linux implementation now uses file based locks instead of shared memory / semaphores, because they seem to be more robust.ยฒ
ยน https://stackoverflow.com/questions/9072570/how-can-i-delete-a-mutex-and-semaphore-in-win32-api
ยฒ http://charette.no-ip.com:81/programming/2010-01-13_PosixSemaphores/index.html