https://github.com/ryan-leap/GreenMeansGoMutexDemo
Demonstrates how to use a Mutex in PowerShell
https://github.com/ryan-leap/GreenMeansGoMutexDemo
interprocess-communication mutex powershell
Last synced: about 1 year ago
JSON representation
Demonstrates how to use a Mutex in PowerShell
- Host: GitHub
- URL: https://github.com/ryan-leap/GreenMeansGoMutexDemo
- Owner: ryan-leap
- License: mit
- Created: 2020-07-19T03:48:32.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-19T21:06:24.000Z (almost 6 years ago)
- Last Synced: 2024-11-07T00:39:10.596Z (over 1 year ago)
- Topics: interprocess-communication, mutex, powershell
- Language: PowerShell
- Homepage:
- Size: 3.13 MB
- Stars: 26
- Watchers: 0
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Green Means Go - A (Visual) Mutex Demo
## What's a Mutex?
A Mutex is an operating system construct which allows processes to coordinate **Mut**ally **Ex**clusive access
to resources. If it were a conversation between the OS and some processes it might sound a bit like this:
- Notepad: "I'd like exclusive access to write to file `C:\Temp\abc.txt`"
- OS: "It's all yours Notepad."
- VSCode: "I'd like exclusive access to write to file `C:\Temp\abc.txt`"
- OS: "Sit tight VSCode. It's being used by someone else."
- VSCode: "Okay - let me know when it's ready."
- OS: "Will do."
- Notepad: "I'm done with file `C:\Temp\abc.txt`"
- OS: "VSCode? File `C:\Temp\abc.txt` is all yours."
- VSCode: "Thanks!"
## Why do I care?
More 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
worker-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.
## Okay, how do I use a Mutex in PowerShell?
Because 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
we can create a Mutex object in PowerShell:
```powershell
$mutex = New-Object System.Threading.Mutex($false, $Name, [ref] $createdMutex)
```
and 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.
## Demo, please.
### The Setup
Each 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.
### Watch a Mutex in action (Green = Owner; Red = Waiting for Ownership)
Separate PowerShell consoles (processes) are using a Mutex to coordinate activity:

### The (Common) Log File
Notice 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.

## Conclusion
A 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!