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
Andrew Smart (2020). semaphore_POSIX_and_Windows (https://www.mathworks.com/matlabcentral/fileexchange/45504-semaphore_posix_and_windows), MATLAB Central File Exchange. Retrieved .
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 .
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.