Skip to content

Latest commit

 

History

History
93 lines (74 loc) · 2.74 KB

README.md

File metadata and controls

93 lines (74 loc) · 2.74 KB

💂 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