File Exchange Pick of the Week

Our best user submissions

Safer Semaphore

Sean‘s pick this week is Safer wrapper for Windows-API Semaphore by Igor.

A while back, I wrote a blog post about the Windows POSIX Semaphore function. This had provided me with a way to make sure that certain processes had finished inside of parallel for-loops to avoid resource contention issues.

At the bottom, there was a caution to:

Use try/catch around the operations you’re doing. If the process fails,
you want to release the semaphore to unblock the other workers. This can
cause a hang if an error occurs because the loop will never complete as
other workers are held.  If you reach this state, force kill the parallel
pool from the task manager.

Igor has developed a class that wraps around the semaphore to provide a timeout in case of this happening!

It does a few other things too:

  • Take key as a character name rather than a number.
  • Make unique key names.
  • Set the maximum number of users in case the resource can have more than one user at once.

And to top that off, Igor provided a nice demo of the functionality:

i=1 	 time=0000.048 	 p1 
i=3 	 time=0000.054 	 p1 
i=1 	 time=0005.050 	 p2 - before wait 
i=3 	 time=0005.054 	 p2 - before wait 
i=1 	 time=0005.051 	 p3 - token granted 
i=1 	 time=0006.056 	 p4 - token released 
i=3 	 time=0005.055 	 p3 - token granted 
i=3 	 time=0006.071 	 p4 - token released 
i=1 	 time=0011.057 	 p5 - finished 
i=3 	 time=0011.079 	 p5 - finished 
i=5 	 time=0011.061 	 p1 
i=5 	 time=0016.071 	 p2 - before wait 
i=2 	 time=0011.080 	 p1 
i=2 	 time=0016.086 	 p2 - before wait 
i=5 	 time=0016.072 	 p3 - token granted 
i=5 	 time=0017.076 	 p4 - token released 
i=2 	 time=0016.089 	 p3 - token granted 
i=2 	 time=0017.092 	 p4 - token released 
i=5 	 time=0022.078 	 p5 - finished 
i=2 	 time=0022.094 	 p5 - finished 
i=4 	 time=0022.078 	 p1 
i=4 	 time=0027.089 	 p2 - before wait 
i=7 	 time=0022.098 	 p1 
i=7 	 time=0027.105 	 p2 - before wait 
i=4 	 time=0027.090 	 p3 - token granted 
i=4 	 time=0028.092 	 p4 - token released 
i=7 	 time=0027.106 	 p3 - token granted 
i=7 	 time=0028.121 	 p4 - token released 
i=4 	 time=0033.105 	 p5 - finished 
i=7 	 time=0033.121 	 p5 - finished 
i=8 	 time=0033.109 	 p1 
i=8 	 time=0038.110 	 p2 - before wait 
i=6 	 time=0033.121 	 p1 
i=6 	 time=0038.125 	 p2 - before wait 
i=8 	 time=0038.111 	 p3 - token granted 
i=8 	 time=0039.127 	 p4 - token released 
i=6 	 time=0038.126 	 p3 - token granted 
i=6 	 time=0039.143 	 p4 - token released 
i=8 	 time=0044.133 	 p5 - finished 
i=6 	 time=0044.148 	 p5 - finished 

My only suggestion for Igor would be to use a check on the number of input arguments with nargin rather than exist(____, 'var') because the latter requires introspection hurting performance.


Give it a try and let us know what you think here or leave a comment for Igor.

Published with MATLAB® R2018b

  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.