File Exchange

image thumbnail

semaphore_POSIX_and​_Windows

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

3 Downloads

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: http://www.mathworks.com/matlabcentral/fileexchange/32489-semaphore
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 (https://www.mathworks.com/matlabcentral/fileexchange/45504-semaphore_posix_and_windows), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (5)

Igor

Jay,
I've modified this code a bit, in particular, I've added an option to set timeout, see : https://www.mathworks.com/matlabcentral/fileexchange/67848-safer-wrapper-for-windows-api-semaphore .

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?

Jay

Jay

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!

Updates

1.3.0.0

Apparently I needed to re-enter contribution ids.

1.2.0.0

Updated the semaphore.c file to fix bugs.

1.1.0.0

Updated description.

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

Inspired by: semaphore