Re: when is the destructor called ?
Your thread can post a message to the GUI's main window when it is about to close.
Note also that there is a WaitForSingleObjectEx call that allows QueueUserAPC calls. QueueUserAPC allows threads to actually queue function-calls which will be called in order when the thread reaches an idle state.
This actually makes a very good messaging system. I usually use a standard APC function which is made typesafe using an abstract base class which is cast then cast back and then its method gets called. You then call the pointer's release() method when you've finished with it. The downside is that when the thread is closing it has to purge its queue to avoid memory leaks.
Re: when is the destructor called ?
I never studied multi-threading in c++ before.
I'll go down to the book store and look at a few books.
But would you happen to have a simple code sample which demonstrates what you are sharing.
Stephen
Re: when is the destructor called ?
Quote:
Originally Posted by stephenprogrammer07
would you be willing to provide a code sample of each ? .
Do you want code samples that demonstrate multi threading in GUI for
game programming? Follow this link
http://www.ogre3d.org/
Re: when is the destructor called ?
maybe i found a good example of proper multhreaded programming at http://www.relisoft.com/win32/active.html
let me know if anyone else knows of a great tutorial.
Re: when is the destructor called ?
Here's how to use MsgWaitForMultipleObjects to do the loop in main
Code:
#define NUM_THREADS 1
int __stdcall WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
/*
* set up GUI here
*/
ThreadManager tmanager;
/*
* Launch first thread
*/
tmanager.launchThread(&tmanager);
/*
* The message loop lasts until we get a WM_QUIT message,
* upon which we shall return from main.
*/
while (true)
{
MSG msg;
bool wmQuit = false;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// If it is a quit message, exit.
if (msg.message == WM_QUIT) {
wmQuit = true;
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (wmQuit)
break;
HANDLE handles[NUM_THREADS];
handles[0] = tmanager.TheHandle();
if (handles[0] == NULL) {
WaitMessage();
continue;
}
DWORD result = MsgWaitForMultipleObjects(
NUM_THREADS,
&handles[0],
FALSE,
INFINITE,
QS_ALLINPUT);
if (result == (WAIT_OBJECT_0 + NUM_THREADS)) {
// New messages have arrived.
continue;
} else {
// Thread has completed
tmanager.close();
tmanager.launchThread(&tmanager);
}
}
tmanager.shuttingDown();
return 0;
}
Re: when is the destructor called ?
Thanks I really appreciate the code example.
This looks a lot simpler than all the classes I had to make to get Mutex and Locks working. I'm studying it.
By the way, in my threadmanager class I didn't have
close()
shuttingdown()
what kind of code did you envison in close() and shuttingDown()
Thanks
Stephen
Re: when is the destructor called ?
Quote:
Originally Posted by stephenprogrammer07
what kind of code did you envison in close() and shuttingDown()
Code:
ThreadManager::~ThreadManager()
{
if (m_hThread != NULL)
CloseHandle(m_hThread);
};
void ThreadManager::close()
{
if (m_hThread != NULL) {
CloseHandle(m_hThread);
m_hThread = NULL;
}
}
void ThreadManager::shuttingDown()
{
if (m_hThread != NULL) {
/*
* If you create an alternate mechanism for cleanly
* shutting down the thread, use it instead of TerminateThread
*/
TerminateThread(m_hThread, 0);
/*
* wait for it to finish, but not forever
* (this isn't really necessary with TerminateThread, but with
* a clean shutdown mechanism, wait for it)
*/
WaitForSingleObject(m_hThread, 10000);
}
}
Re: when is the destructor called ?
Hi googler.
I've been studying on msdn2 about these functions you've presented.
I have a couple questions and observations about your code.
1.) [OBSERVATION] Regarding while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
When I run it as is, PeekMessage never finds anything in the thread message queue. So there is never anything to translate and dispatch. If I remove the entire while loop, the program runs the same.
But I'm guessing the point of this is to show me that if I wanted to Post or Send a Message to this thread message queue, it would translate and dispatch it.
2) [QUESTION] What kind of Msg can be dispatched to the thread ? would that be messages to either kill the thread or parameters for the thread to work upon?
3.) [QUESTION] According to MSDN2 "The DispatchMessage function dispatches a message to a window procedure." Which procedure are you dispatching this message to in your code --- DispatchMessage(&msg); ?
4.) [QUESTION] Is handles[0] hardcoded because you are coding to a scenario that requires just 1 thread at a time ? If there were multiple threads in the queue and the 3rd signaled (that it was finished ) then result would return 3 , then maybe you would have to work upon handles[3]? Let me know.
4b. I noticed that when I set #define NUM_THREADS 3 I get some hard to explain behavior are you able to clarify that behavior ?
4c. Referring to the code block below : handles[0] = tmanager.TheHandle(); always returns Not Null -- I guess this is because tmanager defined outside of the while and closed much later on in the code.
Code:
HANDLE handles[NUM_THREADS];
handles[0] = tmanager.TheHandle();
if (handles[0] == NULL) {
WaitMessage();
continue;
}
How does this solution compare to the use of Mutex etc.
I was trying to experiment with Mutex as seen form this website
http://www.relisoft.com/win32/active.html
and
http://www.relisoft.com/win32/watcher.html
Stephen