"JVM is not running" Error When m file is called in thread function
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi, All. I created a thread function through mexFunction and tried m file in the thread by using mexCallMATLABWithTrap function. However, it did not properly work. I found the error message through the return value of mexCallMATLABWithTrap. It says, "JVM is not running". Why this happens? Does anybody know about this? Is it technically impossible to call m file from the C++ thread function?
As a reference, here is my summarized files.
1. threadtest.cpp
void mexFunction(int nlhs, mxArray *plhs, int nrhs,
mxArray *prhs[])
{
HANDLE hThread;
unsigned threadID;
void* pointer;
hThread = (HANDLE)_beginthreadex( NULL, 0, &MyThreadFunc, pointer, 0, &threadID );
WaitForSingleObject( hThread, INFINITE );
// Destroy the thread object.
CloseHandle( hThread );
}
unsigned __stdcall MyThreadFunc(void * pointer)
{
mexPrintf("I am in thread!!!!\n");
mxArray *returnvalue = mexCallMATLABWithTrap(0, NULL, 0, NULL, "test");
if (returnvalue)
{
mexPrintf("I am here!!!!!\n");
mxArray *plhs = mxGetProperty(returnvalue, 0, "message");
char *buf;
int buflen;
int status;
buflen = (mxGetM(plhs) * mxGetN(plhs)) + 1;
buf = (char*)mxCalloc(buflen, sizeof(char));
if (buf == NULL)
mexErrMsgTxt("Not enough heap space to hold converted string.");
status = mxGetString(plhs, buf, buflen);
if (status == 0)
mexPrintf("The converted string is \n%s.\n", buf);
else
mexErrMsgTxt("Could not convert string data.");
}
_endthreadex( 0 );
return 0;
}
2. test.m
function test()
for i = 1 : 10
fprintf('haha!!!!!!!!!!!!!!!!!!!!\n');
end
Thanks in advance!
0 Commenti
Risposta accettata
Richard Alcock
il 31 Mar 2011
It's fine to create new threads in MEX file, but accessing MATLAB in anyway, including making calls to the MEX API functions, from the spawned thread is not supported.
Più risposte (3)
Jan
il 31 Mar 2011
My Matlab 2009a on WinXP-32bit produces a dramatical crash, when I call mexCallMATLAB from different threads.
The article mentioned by Richard is very interesting: You can run a C-thread in parallel, as long as you do not call Matlab API-Functions. But I do not find a documentation about which function should be avoided exactly: mxGetPr? mxCreateDoubleScalar? mxMalloc? mxGetNaN? Multi-threading is fragile and an exact documentation of thread-safe functions would be very helpful.
2 Commenti
Richard Alcock
il 31 Mar 2011
@Jan - I read "the MEX API is not thread safe" in the link to mean that none of mx* and mex* functions are thread-safe.
Jan
il 31 Mar 2011
@Richard: and if the mx* functions are inlined by "mex -inline"?
All modern processors used for scientific computing have multiple cores. Matlab should be used for scientific computing. Therefore Matlab needs a thread save API. Let's see the new features of 2011a.
Walfredo
il 15 Apr 2011
Hello Brian, I have the same problem. Most of the time, if I try a second time, whatever mexCallMATLAB called would work. I thought it would be ok, but just know, one of my mexCallMATLAB is always failing.
0 Commenti
weike yin
il 17 Apr 2011
????MATLAB engine????????????? ------------------------- You can try to use MATLAB engine, Is't can eval m file in mex spawned thread.
0 Commenti
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!