An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

๐Ÿ’‚ guardpp



A C++17 library for single-instance applications



Linux Build


Windows Build


License

---

## โš™๏ธ 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