"JVM is not running" Error When m file is called in thread function
古いコメントを表示
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!
採用された回答
その他の回答 (3 件)
Jan
2011 年 3 月 31 日
1 投票
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
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
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
2011 年 4 月 15 日
0 投票
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
2011 年 4 月 17 日
0 投票
????MATLAB engine????????????? ------------------------- You can try to use MATLAB engine, Is't can eval m file in mex spawned thread.
カテゴリ
ヘルプ センター および File Exchange で Write C Functions Callable from MATLAB (MEX Files) についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!