Original Post
Ahoy shipmates!
I'm using the Win32 API for this 'un.
Basically what I have is a main thread which creates a child thread. Now, the child thread won't get it's parameters because the creating function returns before the thread gets started. Normally I'd use something like WaitForSingleObject() to get around this, but the child does a few assignments and enters an essentially infinite loop, so what I need is for the main thread to continue on about it's jolly business after the assignments have been done at the start of the child process. I've tried using events and mutex's, but I can't get 'em working. I'll post what I've got:
Obviously I've cut out anything irrelevant. I've tried using OpenEvent(), but this succeeds when I try something like OpenEvent(EVENT_MODIFY_STATE, TRUE, TEXT("AssignEvt")) or (OpenEvent(READ_CONTROL, FALSE, TEXT("AssignEvt")) but fails with error code 5 'Access denied' when I try (OpenEvent(PROCESS_ALL_ACCESS, FALSE, TEXT("AssignEvt")). I'm not sure I ultimately even need to use OpenEvent, that's why I'm here. I've also tried DuplicateHandle() with little success. I may just be doing something wrong.
In any case, even if SetEvent() is hit, it doesn't seem to make a difference to the WaitForSingleObject() in the main thread. Posiible it's using different handles? I've tried passing them through to the child thread too, but that didn't work. Any help would be greatly appreciated.
Oh, by the way, I'm not supposed to be using any special libraries or anything for this project.
I'm using the Win32 API for this 'un.
Basically what I have is a main thread which creates a child thread. Now, the child thread won't get it's parameters because the creating function returns before the thread gets started. Normally I'd use something like WaitForSingleObject() to get around this, but the child does a few assignments and enters an essentially infinite loop, so what I need is for the main thread to continue on about it's jolly business after the assignments have been done at the start of the child process. I've tried using events and mutex's, but I can't get 'em working. I'll post what I've got:
//HEADER
HANDLE g_assignEvt;
static DWORD ThreadFunc(void *p_param)
{
((Collision*)p_param)->ApplyEffect(p_param);
return 1; // Return error
}
//MAIN THREAD
HANDLE Collision::Start(ColParams p_params)
{
g_assignEvt = CreateEvent(NULL,TRUE,0,TEXT("AssignEvt"));
DWORD m_threadID = 0;
LPVOID* m_argList = (LPVOID*)&p_params;
g_thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,m_argList,0,&m_threadID);
WaitForSingleObject(g_assignEvt, INFINITE); //Main thread can't seem to detect that the child has triggered the event, so this just waits forever
CloseHandle(g_assignEvt);
return g_thread;
}//Start
//CHILDTHREAD
void Collision::ApplyEffect(void* p_params)
{
ColParams* m_temp = (ColParams*)p_params;
ColParams m_params = *m_temp;
const unsigned int p_planeCount = *m_params.planeCount;
const unsigned int p_sphereCount = *m_params.sphereCount;
Plane* p_plane = m_params.planes;
Sphere* p_sphereArr = m_params.spheres;
float p_time = *m_params.time;
bool* m_terminate = m_params.terminate;
SetEvent(g_assignEvt); //Assignments are done
...
while(!g_terminated)
{
...
}
}
Obviously I've cut out anything irrelevant. I've tried using OpenEvent(), but this succeeds when I try something like OpenEvent(EVENT_MODIFY_STATE, TRUE, TEXT("AssignEvt")) or (OpenEvent(READ_CONTROL, FALSE, TEXT("AssignEvt")) but fails with error code 5 'Access denied' when I try (OpenEvent(PROCESS_ALL_ACCESS, FALSE, TEXT("AssignEvt")). I'm not sure I ultimately even need to use OpenEvent, that's why I'm here. I've also tried DuplicateHandle() with little success. I may just be doing something wrong.
In any case, even if SetEvent() is hit, it doesn't seem to make a difference to the WaitForSingleObject() in the main thread. Posiible it's using different handles? I've tried passing them through to the child thread too, but that didn't work. Any help would be greatly appreciated.
Oh, by the way, I'm not supposed to be using any special libraries or anything for this project.