Accesing interprocess mutex in simulink S-function.
显示 更早的评论
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 个评论
Erik Stoltenborg
2012-6-11
Kaustubha Govind
2012-6-11
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
2012-6-12
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB Mobile 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!