{"id":27306447,"url":"https://github.com/bcuff/asyncprimitives","last_synced_at":"2025-04-12T03:59:59.174Z","repository":{"id":15647223,"uuid":"18384409","full_name":"bcuff/AsyncPrimitives","owner":"bcuff","description":"Async concurrency primitives for .NET","archived":false,"fork":false,"pushed_at":"2018-05-31T20:10:19.000Z","size":48,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-12T03:59:55.246Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bcuff.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-02T22:54:35.000Z","updated_at":"2018-05-31T20:10:21.000Z","dependencies_parsed_at":"2022-09-12T23:21:18.124Z","dependency_job_id":null,"html_url":"https://github.com/bcuff/AsyncPrimitives","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcuff%2FAsyncPrimitives","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcuff%2FAsyncPrimitives/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcuff%2FAsyncPrimitives/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bcuff%2FAsyncPrimitives/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bcuff","download_url":"https://codeload.github.com/bcuff/AsyncPrimitives/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248514225,"owners_count":21116901,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2025-04-12T03:59:58.041Z","updated_at":"2025-04-12T03:59:59.163Z","avatar_url":"https://github.com/bcuff.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"AsyncPrimitives\n===============\n\nAsync concurrency primitives for .NET\n\n## Async Semaphore\n\n### Methods\n\n#### Wait\n`public Task Wait()`\nWaits until the count is greater than 0 then enters the semaphore. Decrements the count upon entry.\n\n#### WaitAndRelease\n`public Task\u003cIDisposable\u003e WaitAndRelease()`\nWaits (as above) then returns an `IDisposable`. When the return value is disposed `void Release()` is called.\n\n#### Release\n`public void Release(int count = 1)`\nIncrements the count by the specified number and releases the corresponding number of waiters (if there are any waiters).\n\n```c#\npublic class SomeClass\n{\n    private readonly _semaphore = new AsyncSemaphore(3 /* starting count */);\n    \n    public async Task DoSomethingAsync()\n    {\n        using(await _semaphore.WaitAndRelease())\n        {\n            // only 3 callers can run this at a time\n            // ...\n        }\n    }\n}\n```\n\n## Async ReaderWriterLock\n```c#\n_gate = new AsyncReaderWriterLock();\n\npublic async Task ModifyResource()\n{\n    using(await _gate.OpenWriter())\n    {\n        // ... do something to modify a protected resource ...\n    }\n}\n\npublic async Task\u003cstring\u003e GetResource()\n{\n    using(await _gate.OpenReader())\n    {\n        // ... do something to obtain a protected resource ...\n    }\n}\n```\n\n## AsyncLazy\u0026lt;T\u0026gt;\n```c#\nvar lazy = new AsyncLazy\u003cstring\u003e(async () =\u003e\n{\n\tusing(var client = new WebClient())\n\t{\n\t\treturn await client.DownloadStringTaskAsync(\"http://google.com\");\n\t}\n});\n\n// first time actually downloads http://google.com\nvar html = await lazy;\n\n// second time gets it from memory\nvar html2 = await lazy;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcuff%2Fasyncprimitives","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbcuff%2Fasyncprimitives","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbcuff%2Fasyncprimitives/lists"}