Giter Club home page Giter Club logo

greenmeansgomutexdemo's Introduction

Green Means Go - A (Visual) Mutex Demo

What's a Mutex?

A Mutex is an operating system construct which allows processes to coordinate Mutally Exclusive 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 or ThreadJobs) 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 class. With that we can create a Mutex object in 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. 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:

Green Means Go Mutex Demo Consoles

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.

Green Means Go Mutex Demo Log File

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!

greenmeansgomutexdemo's People

Contributors

ryan-leap avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.