Skip to content

Trivial implementation of a lockfile class for simple singletons

License

Notifications You must be signed in to change notification settings

SiddiqSoft/DaemonLockfile

Repository files navigation

DaemonLockfile : A simple lockfile implementation for modern C++

CodeQL Build Status

Objective

Provide for daemons (applications running as services under docker as an example) to limit themselves to a single instance per "name".

Requirements

  • C++20
  • Useful for running "daemons" under docker where you may not always have or wish for a true service under the operating system.

Usage

  • Use the nuget SiddiqSoft.DaemonLockfile
  • You can also git submodule: git submodule add https://github.com/SiddiqSoft/DaemonLockfile.git
  • Copy paste..whatever works.

Example (when using nuget to add the header in the solution)

#include "siddiqsoft/DaemonLockfile.hpp"


// Example daemon host structure
struct MyDaemonType
{
	siddiqsoft::DaemonLockfile myLockfile{"MyDaemonType", true}; //force acquire
	..
	..
};

// The "daemon" will hold the lock and when the application exits the lock will be released.
static MyDaemonType daemon;


int main(int argc, char *argv[])
{
	// Bailout immediately if we do not have a lock--this implies some other instance is running
	if(!daemon.myLockfile.isLocked) return 1;
	..
	..
	// We have the exclusice lock
}

Roadmap

© 2021 Siddiq Software LLC. All rights reserved.