🔒 Locked
Multithread programming problem
Advertisement
Original Post
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
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 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.
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.
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.
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:
so now you can tell the thread to terminate by setting the
event (using SetEvent()). Thus your main can look a bit like:
You could of course bundle the data with the event in a struct and pass it to the thread instead, eliminating the global hExitEvent.
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.
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.
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.
Advertisement