Azzera filtri
Azzera filtri

"JVM is not running" Error When m file is called in thread function

3 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Richard Alcock
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.
  2 Commenti
Brian Kim
Brian Kim il 31 Mar 2011
I really appreciate your clear answer. I hope the flexible accessing from threads to MATLAB is supported soon.
Brian Kim
Brian Kim il 31 Mar 2011
I really appreciate your clear answer. I hope the flexible accessing from threads to MATLAB is supported soon.

Accedi per commentare.

Più risposte (3)

Jan
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
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
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.

Accedi per commentare.


Walfredo
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.

weike yin
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.

Categorie

Scopri di più su Programming 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!

Translated by