https://github.com/soundux/guardpp
๐ A cross-platform C++17 library that can restrict your application to a single instance
https://github.com/soundux/guardpp
Last synced: about 2 months ago
JSON representation
๐ A cross-platform C++17 library that can restrict your application to a single instance
- Host: GitHub
- URL: https://github.com/soundux/guardpp
- Owner: Soundux
- License: mit
- Created: 2021-05-29T00:25:02.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-05-19T16:02:11.000Z (about 4 years ago)
- Last Synced: 2025-01-12T00:13:36.166Z (over 1 year ago)
- Language: C++
- Homepage:
- Size: 17.6 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
๐ guardpp
A C++17 library for single-instance applications
---
## โ๏ธ Configuration
### Tests
```cmake
set(guardpp_tests OFF)
```
> If set to `ON`, guardpp will build a test executable.
## ๐ Installation
- FetchContent
```cmake
include(FetchContent)
FetchContent_Declare(lockpp GIT_REPOSITORY "https://github.com/Soundux/guardpp")
FetchContent_MakeAvailable(guardpp)
target_link_libraries( guardpp)
```
- Git Submodule
```bash
git submodule add "https://github.com/Soundux/guardpp"
```
```cmake
# Somewhere in your CMakeLists.txt
add_subdirectory("")
target_link_libraries( guardpp)
```
## ๐ Usage
### Example
```cpp
#include
#include
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();
}
```
## ๐ Dependencies
- [`expected`](https://github.com/TartanLlama/expected)
- [`tiny-process-library`](https://gitlab.com/eidheim/tiny-process-library) _(For tests only!)_
## โน๏ธ Remarks
- `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