matshare
is a subsystem for MATLAB R2008b to R2017b which provides functions for creating and operating on shared memory. It has mechanisms for automatic garbage collection, thread-safe memory creation, and in-place overwriting.
MATLAB R2008b to R2017b on Windows and Linux. This is not available for R2018a+ yet because of changes made the MEX API. This is also not available for MacOS partially because of problems with locking files but mainly because I don't own a Mac.
First pick up the latest release. You may need to compile matshare
if you are running Linux, in which case just navigate to source
and run INSTALL.m
.
>> matshare.share(rand(3));
>> fetched = matshare.fetch fetched = matshare object storing 3x3 double: data: [3×3 double] >> fetched.data ans = 0.814723686393179 0.913375856139019 0.278498218867048 0.905791937075619 0.63235924622541 0.546881519204984 0.126986816293506 0.0975404049994095 0.957506835434298
>> shared = matshare.share('-n', 'myvarname', 1); >> shared.data ans = 1
>> f = matshare.fetch('myvarname') f = matshare object storing 1x1 double: data: 1 >> f.data(1) = 2;
>> shared.data ans = 2
>> shared = matshare.share(1); >> shared.data ans = 1
>> f = matshare.fetch f = matshare object storing 1x1 double: data: 1 >> f.add(2);
>> shared.data ans = 3
This, and all the other operations can be made synchronous through mutexes or lockfree intrinsics.
For more details, you can navigate through the documentation with help matshare
or help matshare.[command]
, replacing [command]
with any one of the matshare
commands.
This project is pretty much complete save for the inevitable bugs. However these may be implemented at some point in the future:
Variable identifiers (being able to select shared variables by name)Atomic variable operations (increments, decrements, swaps, etc.)Variable overwriting by indexThread locks on a sub-global level- Rewrite in C++ for R2018a+
- User defined locks/waits
- Direct usage of matshare objects without the 'data' property
If you wish to contribute or have any suggestions feel free to contact me.
This project is licensed under the MIT license.