{"id":26155457,"url":"https://github.com/ntdls/ntdls.semaphore","last_synced_at":"2026-02-12T02:09:09.267Z","repository":{"id":199298297,"uuid":"702594121","full_name":"NTDLS/NTDLS.Semaphore","owner":"NTDLS","description":"Provides various classes for to ensure sequential mult-threaded access to variables or sections of code.","archived":false,"fork":false,"pushed_at":"2025-10-27T04:49:32.000Z","size":237,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-27T06:24:45.843Z","etag":null,"topics":["library","nuget","threading"],"latest_commit_sha":null,"homepage":"https://networkdls.com/Entity/ntdls-semaphore","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NTDLS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-09T15:57:40.000Z","updated_at":"2025-10-27T04:49:27.000Z","dependencies_parsed_at":"2023-11-08T04:02:18.550Z","dependency_job_id":"0e7d949a-0537-43c1-a2bc-48011a884011","html_url":"https://github.com/NTDLS/NTDLS.Semaphore","commit_stats":null,"previous_names":["ntdls/ntdls.semaphore"],"tags_count":35,"template":false,"template_full_name":null,"purl":"pkg:github/NTDLS/NTDLS.Semaphore","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NTDLS%2FNTDLS.Semaphore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NTDLS%2FNTDLS.Semaphore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NTDLS%2FNTDLS.Semaphore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NTDLS%2FNTDLS.Semaphore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NTDLS","download_url":"https://codeload.github.com/NTDLS/NTDLS.Semaphore/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NTDLS%2FNTDLS.Semaphore/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29354701,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-12T01:03:07.613Z","status":"online","status_checked_at":"2026-02-12T02:00:06.911Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["library","nuget","threading"],"created_at":"2025-03-11T08:56:16.370Z","updated_at":"2026-02-12T02:09:09.263Z","avatar_url":"https://github.com/NTDLS.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NTDLS.Semaphore\r\n\r\n📦 Be sure to check out the NuGet package: https://www.nuget.org/packages/NTDLS.Semaphore\r\n\r\n## Pessimistic Critical Resource\r\nProvides various classes to protect a variable from parallel / non-sequential thread access by always acquiring an exclusive lock on the resource.\r\nAlso allows for shared access, pessimistic locking, optimistic locking and dead-lock prevention lock patterns with lightweight cancellation.\r\n\r\n**PessimisticCriticalResource using inline execution example:**\r\n\u003eAn example using a PessimisticCriticalResource to envelope a variable and protect it from parallel execution,\r\n\u003e Note that there are nullable and non-nullable counterparts and also template/generics of each method to\r\n\u003e allow you to return various types from the delegate execution.\r\n```csharp\r\npublic class Car\r\n{\r\n    public string? Name { get; set; }\r\n    public int NumerOhWheels { get; set; }\r\n}\r\n\r\npublic PessimisticCriticalResource\u003cList\u003cCar\u003e\u003e Cars { get; set; } = new();\r\n\r\npublic void Add(Car car)\r\n{\r\n    Cars.Use((obj) =\u003e obj.Add(car));\r\n}\r\n\r\npublic Car? GetByName(string name)\r\n{\r\n    return Cars.Use((obj) =\u003e obj.Where(o=\u003eo.Name == name).FirstOrDefault());\r\n}\r\n\r\npublic bool TryAdd(Car car)\r\n{\r\n    //Since TryUse\u003cT\u003e can return values, we have to pass the result of the try out though a variable.\r\n    Cars.TryUse(out bool wasLockObtained, (obj) =\u003e obj.Add(car));\r\n    return wasLockObtained;\r\n}\r\n\r\npublic bool TryAdd(Car car, int timeout)\r\n{\r\n    //Since TryUse\u003cT\u003e can return values, we have to pass the result of the try out though a variable.\r\n    Cars.TryUse(out bool wasLockObtained, timeout, (obj) =\u003e obj.Add(car));\r\n    return wasLockObtained;\r\n}\r\n```\r\n\r\n\r\n**Multi PessimisticCriticalResource using inline execution example:**\r\n\u003eAn example using a PessimisticCriticalResource to envelope a variable and protect it and others from parallel execution.\r\n```\r\npublic class Car\r\n{\r\n    public string? Name { get; set; }\r\n    public int NumerOhWheels { get; set; }\r\n}\r\n\r\npublic PessimisticCriticalResource\u003cList\u003cCar\u003e\u003e Cars { get; set; } = new();\r\npublic PessimisticSemaphore OtherLock1 { get; set; } = new();\r\npublic PessimisticSemaphore OtherLock2 { get; set; } = new();\r\npublic PessimisticSemaphore OtherLock3 { get; set; } = new();\r\n\r\npublic void Add(Car car)\r\n{\r\n    Cars.Use((obj) =\u003e obj.Add(car));\r\n}\r\n\r\npublic Car? GetByName(string name)\r\n{\r\n    return Cars.Use((obj) =\u003e obj.Where(o =\u003e o.Name == name).FirstOrDefault());\r\n}\r\n\r\npublic bool TryAdd(Car car)\r\n{\r\n    //Since TryUse\u003cT\u003e can return values, we have to pass the result of the try out though a variable.\r\n    Cars.TryUse(out bool wasLockObtained, (obj) =\u003e obj.Add(car));\r\n    return wasLockObtained;\r\n}\r\n\r\npublic bool TryAdd(Car car, int timeout)\r\n{\r\n    //Since TryUse\u003cT\u003e can return values, we have to pass the result of the try out though a variable.\r\n    Cars.TryUse(out bool wasLockObtained, timeout, (obj) =\u003e obj.Add(car));\r\n    return wasLockObtained;\r\n}\r\n\r\npublic Car? TryGet(string name, int timeout)\r\n{\r\n    return Cars.TryUseAll(new[] { OtherLock1, OtherLock2, OtherLock3 }, timeout, out bool wasLockObtained, (obj) =\u003e\r\n    {\r\n        //We only get here if we are able to lock \"Cars\" and OtherLock1, OtherLock2 and OtherLock3\r\n        return obj.Where(o =\u003e o.Name == name).FirstOrDefault();\r\n    });\r\n}\r\n```\r\n\r\n\r\n## Optimistic Critical Resource\r\nProtects a variable from parallel / non-sequential thread access but controls read-only and exclusive\r\naccess separately to prevent read operations from blocking other read operations.it is up to the developer\r\nto determine when each lock type is appropriate. Note: read-only locks only indicate intention, the resource\r\nwill not disallow modification of the resource, but this will lead to race conditions.\r\n\r\n**OptimisticCriticalResource using inline execution example:**\r\n\u003e\r\n\u003eAn example using a OptimisticCriticalResource to protect a portion of code from parallel execution while not allowing reads to block reads.\r\n```csharp\r\npublic class Car\r\n{\r\n    public string? Name { get; set; }\r\n    public int NumerOhWheels { get; set; }\r\n}\r\n\r\npublic OptimisticCriticalResource\u003cList\u003cCar\u003e\u003e Cars { get; set; } = new();\r\n\r\npublic void Add(Car car)\r\n{\r\n    Cars.Write((obj) =\u003e obj.Add(car));\r\n}\r\n\r\npublic Car? GetByName(string name)\r\n{\r\n    return Cars.Read((obj) =\u003e obj.Where(o=\u003eo.Name == name).FirstOrDefault());\r\n}\r\n\r\npublic bool TryAdd(Car car)\r\n{\r\n    //Since TryUse\u003cT\u003e can return values, we have to pass the result of the try out though a variable.\r\n    Cars.TryWrite(out bool wasLockObtained, (obj) =\u003e obj.Add(car));\r\n    return wasLockObtained;\r\n}\r\n\r\npublic bool TryAdd(Car car, int timeout)\r\n{\r\n    //Since TryUse\u003cT\u003e can return values, we have to pass the result of the try out though a variable.\r\n    Cars.TryWrite(out bool wasLockObtained, timeout, (obj) =\u003e obj.Add(car));\r\n    return wasLockObtained;\r\n}\r\n```\r\n\r\n\r\n## Critical Section\r\nProtects an area of code from parallel / non-sequential thread access.\r\n\r\n**PessimisticSemaphore using inline execution example:**\r\n\u003e\r\n\u003eAn example using a PessimisticSemaphore to protect a portion of code from parallel execution.\r\n```csharp\r\nprivate PessimisticSemaphore _pessimisticSemaphore = new();\r\n\r\nprivate int _value;\r\n\r\npublic int Value\r\n{\r\n    get\r\n    {\r\n        return _pessimisticSemaphore.Use(() =\u003e _value);\r\n    }\r\n    set\r\n    {\r\n        _pessimisticSemaphore.Use(() =\u003e _value = value);\r\n    }\r\n}\r\n```\r\n\r\n## Thread ownership tracking\r\nIf you need to keep track of which thread owns each semaphore and/or critical sections then\r\n  you can enable \"ThreadOwnershipTracking\" by calling ThreadOwnershipTracking.Enable(). Once this\r\n  is enabled, it is enabled for the life of the application so this is only for debugging\r\n  deadlock/race-condition tracking.\r\nYou can evaluate the ownership by evaluating\r\n  the dictonary \"ThreadOwnershipTracking.LockRegistration\" or and instance of\r\n  \"PessimisticSemaphore\" or \"PessimisticCriticalResource\" CurrentOwnerThread.\r\n\r\n**Enabling Thread Ownership Tracking**\r\n\u003e\r\n\u003eAn example of enabling the thread ownerhsip mechanism.\r\n```csharp\r\nThreadOwnershipTracking.Enable();\r\n```\r\n\r\n## License\r\n[Apache-2.0](https://choosealicense.com/licenses/apache-2.0/)\r\n\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fntdls%2Fntdls.semaphore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fntdls%2Fntdls.semaphore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fntdls%2Fntdls.semaphore/lists"}