{"id":13599084,"url":"https://github.com/ryan-leap/GreenMeansGoMutexDemo","last_synced_at":"2025-04-10T12:31:16.850Z","repository":{"id":217054844,"uuid":"280786527","full_name":"ryan-leap/GreenMeansGoMutexDemo","owner":"ryan-leap","description":"Demonstrates how to use a Mutex in PowerShell","archived":false,"fork":false,"pushed_at":"2020-07-19T21:06:24.000Z","size":3282,"stargazers_count":26,"open_issues_count":0,"forks_count":5,"subscribers_count":0,"default_branch":"master","last_synced_at":"2024-11-07T00:39:10.596Z","etag":null,"topics":["interprocess-communication","mutex","powershell"],"latest_commit_sha":null,"homepage":"","language":"PowerShell","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/ryan-leap.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-19T03:48:32.000Z","updated_at":"2024-08-26T06:08:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"5074c882-e2e9-4590-b03d-f847717a4f5c","html_url":"https://github.com/ryan-leap/GreenMeansGoMutexDemo","commit_stats":null,"previous_names":["ryan-leap/greenmeansgomutexdemo"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryan-leap%2FGreenMeansGoMutexDemo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryan-leap%2FGreenMeansGoMutexDemo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryan-leap%2FGreenMeansGoMutexDemo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryan-leap%2FGreenMeansGoMutexDemo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryan-leap","download_url":"https://codeload.github.com/ryan-leap/GreenMeansGoMutexDemo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248217126,"owners_count":21066633,"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":["interprocess-communication","mutex","powershell"],"created_at":"2024-08-01T17:00:59.552Z","updated_at":"2025-04-10T12:31:16.838Z","avatar_url":"https://github.com/ryan-leap.png","language":"PowerShell","funding_links":[],"categories":["PowerShell"],"sub_categories":[],"readme":"# Green Means Go - A (Visual) Mutex Demo\n\n## What's a Mutex?\n\nA Mutex is an operating system construct which allows processes to coordinate **Mut**ally **Ex**clusive access\nto resources.  If it were a conversation between the OS and some processes it might sound a bit like this:\n\n- Notepad: \"I'd like exclusive access to write to file `C:\\Temp\\abc.txt`\"\n- OS: \"It's all yours Notepad.\"\n- VSCode: \"I'd like exclusive access to write to file `C:\\Temp\\abc.txt`\"\n- OS: \"Sit tight VSCode.  It's being used by someone else.\"\n- VSCode: \"Okay - let me know when it's ready.\"\n- OS: \"Will do.\"\n- Notepad: \"I'm done with file `C:\\Temp\\abc.txt`\"\n- OS: \"VSCode? File `C:\\Temp\\abc.txt` is all yours.\"\n- VSCode: \"Thanks!\"\n\n## Why do I care?\n\nMore than likely you don't. But, if your script does some Asynchronous-Fu (think [Jobs](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_jobs?view=powershell-7) or [ThreadJobs](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_thread_jobs?view=powershell-7)) and those\nworker-jobs share a log file you're gonna ***need*** a mutex.  Otherwise those jobs will be stepping on each other - log entries will be missed...exceptions will be thrown...crying and nashing of teeth sort-of-thing.\n\n## Okay, how do I use a Mutex in PowerShell?\n\nBecause PowerShell is sitting right on top of .NET we can leverage the [System.Threading.Mutex](https://docs.microsoft.com/en-us/dotnet/api/system.threading.mutex?view=netcore-3.1) class.  With that\nwe can create a Mutex object in PowerShell:\n\n```powershell\n$mutex = New-Object System.Threading.Mutex($false, $Name, [ref] $createdMutex)\n```\nand use the methods it provides to wait (`WaitOne`) for exclusive access to a resource and release (`ReleaseMutex`) ownership of that resource when we're done.\n\n## Demo, please.\n\n### The Setup\n\nEach PowerShell console will run script [`Start-GreenMeansGoMutexDemo.ps1`](./Start-GreenMeansGoMutexDemo.ps1).  For several passes (all for demo purposes) the script will request ownership of a named Mutex and if it gets ownership it will change the console color to Green, log a message (to a shared log file) and hang on to the ownership (by sleeping) for a (progressively shorter) period of time.  If the script does **not** get ownership of the Mutex (because it is owned by another) it will change the console color to Red and wait until ownership is granted.\n\n### Watch a Mutex in action (\u003cspan style=\"color:green\"\u003eGreen\u003c/span\u003e = Owner; \u003cspan style=\"color:red\"\u003eRed\u003c/span\u003e = Waiting for Ownership)\n\nSeparate PowerShell consoles (processes) are using a Mutex to coordinate activity:\n\n![Green Means Go Mutex Demo Consoles](./images/green_means_go_mutex_demo.gif)\n\n### The (Common) Log File\n\nNotice that in addition to the console color renderings that these same separate processes are writing to a common log file in a coordinated (via Mutex) fashion.\n\n![Green Means Go Mutex Demo Log File](./images/green_means_go_log_file.png)\n\n## Conclusion\n\nA Mutex is a locking mechanism that allows processes and threads to coordinate access to resources.  You might need it one day - especially if you have scripts that do multi-process (Jobs) or mult-threaded (ThreadJobs) work and require access to the same resource - like a log file.  Enjoy!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryan-leap%2FGreenMeansGoMutexDemo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryan-leap%2FGreenMeansGoMutexDemo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryan-leap%2FGreenMeansGoMutexDemo/lists"}