File Exchange

image thumbnail


version (5.03 KB) by Andrew Smart
Patches Dillon's work to allow native semaphore usage within Matlab on Windows and POSIX systems


Updated 19 Mar 2014

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Augment of Mr. Dillon's work:
This provides an interface with POSIX semaphore functionality on the native platform. Windows doesn't implement the POSIX semaphore API, so the Windows API equivalents are used if compiled on Windows.
To install/compile: mex -O -v semaphore.c

Cite As

Andrew Smart (2020). semaphore_POSIX_and_Windows (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (5)


I've modified this code a bit, in particular, I've added an option to set timeout, see : .

Andrew Smart,
Many thanks for the code!

in non-Win32, semaphore returns a value, the semaphore value (I think). In Win32 it does not return a value--could this be fixed easily?



You may want to add 'trywait' as an option. I originally tried to use this code to protect a file-writer in a GUI that also had a background timer. It appears that the timers run in the same thread as the GUI, and as a result 'wait' may hang the thread if it occurs in both the timer code and the gui code. While I am sure this is less than ideal, 'trywait' allows me to use matlab's sleep to halt execution of the gui or timer while waiting for the lock as opposed to sleeping the entire thread.

This function is awesome, it feels magical :)
Thanks so much!


Apparently I needed to re-enter contribution ids.

Updated the semaphore.c file to fix bugs.

Updated description.

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: semaphore