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

3 次查看(过去 30 天)
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!

采纳的回答

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

请先登录,再进行评论。

更多回答(3 个)

Jan
Jan 2011-3-31
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 个评论
Richard Alcock
Richard Alcock 2011-3-31
@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 2011-3-31
@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
Walfredo 2011-4-15
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 2011-4-17
????MATLAB engine????????????? ------------------------- You can try to use MATLAB engine, Is't can eval m file in mex spawned thread.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by