Skip to content

๐Ÿ’‚ A cross-platform C++17 library that can restrict your application to a single instance

License

Notifications You must be signed in to change notification settings

Soundux/guardpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

7 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ’‚ guardpp

A C++17 library for single-instance applications

Linux Build Windows Build License


โš™๏ธ Configuration

Tests

set(guardpp_tests OFF)

If set to ON, guardpp will build a test executable.

๐Ÿ“Ž Installation

  • 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)

๐Ÿ“” Usage

Example

#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(); 
}

๐Ÿ“š Dependencies

โ„น๏ธ 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

About

๐Ÿ’‚ A cross-platform C++17 library that can restrict your application to a single instance

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published