Accesing interprocess mutex in simulink S-function.

3 views (last 30 days)
I'm trying to communicate with matlab via boost::interprocess, using shared memory in combination with boost::interprocesss::conditions. Much(completly) like the example on the boost website.
A plain c++ .exe file is responable of the creation of the shared memory, all matlab has to is open it and unlock/lock the mutexes and notify.
I can get this to work inside MDL start, this works. But this is obviously not what im looking for, i want to acces the data during simulation:
static void mdlStart(SimStruct *S)
{
using namespace boost::interprocess;
shared_memory_object shm
(open_only //only create
,"MySharedMemory" //name
,read_write //read-write mode
);
try{
//Map the whole shared memory in this process
mapped_region region
(shm //What to map
,read_write //Map it as read-write
);
//Get the address of the mapped region
void * addr = region.get_address();
//Obtain a pointer to the shared structure
trace_queue * data = static_cast<trace_queue*>(addr);
void **PWork = ssGetPWork(S);
PWork[0] = data;
trace_queue * data2;
data2 = (trace_queue *) ssGetPWorkValue(S,0);
}
catch(interprocess_exception &ex){
mexPrintf("Interprocess exeception: %s \n", ex.what());
}
Now i want to acces this data every time step. So i use the PWork vector created above do a simple loop. However the a soon as matlab reaches the first line that acces data (scoped lock) it crashes!
using namespace boost::interprocess;
trace_queue * data2 = (trace_queue *) ssGetPWorkValue(S,0);
bool end_loop = false;
do{
scoped_lock<interprocess_mutex> lock(data2->mutex);
if(!data2->message_in){
data2->cond_empty.wait(lock);
}
if(std::strcmp(data2->items, "last message") == 0){
end_loop = true;
}
else{
//Print the message
mexPrintf("%s \n", data2->items);
//Notify the other process that the buffer is empty
data2->message_in = false;
data2->cond_full.notify_one();
}
}
while(!end_loop);
Nesting this piece code in together with the rest in mdlStart works fine. Same thing goes if i put everything together in mdlInitializeConditions or mdlInitializeSizes. When i do this in mdlOutput or update it crashes.
All cause the same crash, ill post the stacktrace in a reply to keep things a bit more organized.
  3 Comments
Kaustubha Govind
Kaustubha Govind on 11 Jun 2012
I think this might be a broader MEX/MATLAB issue. I don't know too much about boost::interprocess, but from a web search it seems like others have run into similar issues with interprocess and MEX.
Erik Stoltenborg
Erik Stoltenborg on 12 Jun 2012
Yeah, i've noticed the same. Line 8 is the actual problem as far as i can see. One of my collegeas had a simular problem when using plain pthread in output/update, Matlab does not seem to like it at all. A run with valgrind againt pointed to the mutex lock which is the first attempt of accesing the shm.
People suggested building the external against matlab libraries, this didnt have any effect. It was not the problem for it works fine elsewhere in the s-function.
This: http://www.mathworks.com/matlabcentral/fileexchange/28572-sharedmatrix is simular, but is just a shared memory matrix, i would like to use mutex for triggering purposes.
I've just noticed this: http://www.mathworks.com/matlabcentral/fileexchange/32489, from the same author. I will try making something simular inside my s-function. Using this code, or recoding for use with conditions.
Still strange that it will run in intialization/start but not at actual runtime.

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!