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

5 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (3)

Jan
Jan el 31 de Mzo. de 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 comentarios
Richard Alcock
Richard Alcock el 31 de Mzo. de 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 el 31 de Mzo. de 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.

Iniciar sesión para comentar.


Walfredo
Walfredo el 15 de Abr. de 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 el 17 de Abr. de 2011
????MATLAB engine????????????? ------------------------- You can try to use MATLAB engine, Is't can eval m file in mex spawned thread.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by