Skip to main content
gamedev.net
🔒 Locked

Multithread programming problem

Started by Benjamin 9 replies 750+ views
Original Post
Benjamin
Benjamin
I have the following code: WinMain() // win main fucntion { DATA* lpData = new DATA; DWORD ThreadID; CreateThread(NULL,0,Thread1,lpData,0,&ThreadID); delete lpData; return 0; } Thread1() { lpData->Dosomething(); return 0; } Is this code cause any problem? Actually when I delete the lpData, I no longer need Thread1. But I want to know is there any problem about the memory? thanks
Ben
jonnyfish
jonnyfish
I haven''t tried the code, so don''t quote me on this, but lpData is being deleted immediately after the thread is started, which is quite possibly before Thread1() is finished with it.
Benjamin
Benjamin
Yes. that''s what I want to know.
I want to know if I forced to delete a pointer used by another thread which both the thread and the pointer are not critical things, will it caused a memory problem?
Ben
Benjamin
Benjamin
It seems I need first terminate the thread and then delete the pointer. However, is there any method to delete a thread?
Ben
Shannon Barber
Shannon Barber
You can allocate memory in the main thread and use it in the second thread, but you need to take care to syncronize the creation & access & destruction. In this case you would want to create a critical section for controlling access to the lpData.

The main thread would create the cs, lock it, could launch the thread & allocate memory for lpData & unlock the cs. The secondary thread needs to lock the cs before using lpData and unlock the cs when its down.

Usually secondary threads have some sort of while loop and terminate when a key bool flag is set, or when an event is signaled, or in the case of a UI thread when a WM_QUIT message is sent.


...
Even if that code ran once perfectly well, it wont always work perfectly well. It can crash.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Odie
Odie
You may find these win32 functions helpful:

ExitThread(...)
Shuts down the calling thread.

TerminiateThread(...)
Terminiates the a specified thread forcefully.

WaitForSingleObject(...)
Waits for a specified object to enter the signaled state.


You can use WaitForSingleObject(...) in your main() function right after you call the create thread function but before you delete lpData. This will cause the main program thread to wait until Thread1 terminates. You can then safely delete lpData without worrying Thread1 accessing a invalid memory location.
amag
amag
Normally you should never terminate a thread from another thread, but rather tell it to stop running by signaling it through an event (or something similar). Code for the thread might be:


HANDLE hExitEvent = NULL;

void mythread(void *lpData)
{
while(WaitForSingleObject(hExitEvent, 0) == WAIT_TIMEOUT)
{
do_my_stuff();
}
}


so now you can tell the thread to terminate by setting the
event (using SetEvent()). Thus your main can look a bit like:


int WinMain(...)
{
hExitEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
DATA* lpData = new DATA;
HANDLE hThread = (HANDLE)_beginthread(mythread, 0, lpData);

// do some more stuff?

// tell the thread to stop
SetEvent(hExitEvent);
// wait for it to stop
WaitForSingleObject(hThread, INFINITE);

delete lpData;
CloseHandle(hExitEvent);
return 0;
}


You could of course bundle the data with the event in a struct and pass it to the thread instead, eliminating the global hExitEvent.

Benjamin
Benjamin
Thanks all of you.
I completely understand.
Ben
mhkrause
mhkrause
You may want to call WaitForSingleObject on the thread handle (returned by CreateThread). This will cause your thread to wait until the thread exits. Don''t forget to call CloseHandle() on the handle returned from CreateThread().

Also, try to avoid using CreateThread and use _beginthread or _beginthreadex instead. They properly initialize the C library for the new thread. If you don''t need to use the standard C library, you don''t need to bother.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.