unity plugin c++ thread import openmp thread has

my unity project has c++ plugin dll, dll function has openmp thread. OpenMP when used in threads leak thread handles and memory.

C#
//////////////////////////////////////////////////////////////////////////////////////////////////////////
[DllImport(“CRS3D”, EntryPoint = “Main”, CallingConvention = CallingConvention.StdCall)]
public static extern void Main(string args);
///////////////////////////////////////////////////////////////////////////////////////////////////////////
InterfaceDLL.Main(CommandMessage);

C++
////////////////////////////////////////////////////////////////////////////////////////////////////////
#define INTERFACEFUNCDLL_API __declspec(dllexport)
INTERFACEFUNCDLL_API void __stdcall Main(char* args);
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void Main(char* arg)
{
unsigned int dwThreadID;
DWORD dwExitCode = 0;
hThread = (HANDLE)_beginthread(Main_Loop_Proc, 0, (void*)arg);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void Main_Loop_Proc(void param)
{
ret = ThreadProc((char
)param);
TerminateThread(hThread, 0);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
int ThreadProc(void *param)
{
int idx;
int arr[BUFFLEN];
int res = 0;
int mid[NUMTHREAD]; // thread num - next version need to malloc
int nProcessors = omp_get_max_threads() *3;
omp_set_num_threads(nProcessors);
#pragma omp parallel
{
#pragma omp for
for (idx = 0; idx < BUFFLEN; idx++)
arr[idx] = idx;
#pragma omp for
for (idx = 0; idx < NUMTHREAD; idx++)
mid[idx] = 0;
#pragma omp for
for (idx = 0; idx < BUFFLEN; idx++)
{
int tid = omp_get_thread_num();
mid[tid] += arr[idx];
}
#pragma omp single
for (idx = 0; idx < NUMTHREAD; idx++)
res += mid[idx];
}
// omp_set_dynamic(0);
#pragma omp barrier
OutputDebugStringA(“Begin4”);
char s[256];
sprintf(s, “val = %d”, res);
OutputDebugStringA(s);
return -1;
}

So?

I so sorry. this message move Q&A Help Room

http://answers.unity3d.com/questions/1175957/unity-plugin-c-thread-import-openmp-thread-has-lea.html