Re: How to current close window while messages still in queue
"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:clk4h4tmfcfepilqo1garh2n7vh33ga6t3@4ax.com...
On Wed, 5 Nov 2008 12:37:28 -0800, "David Ching"
<dc@remove-this.dcsoft.com> wrote:
"vvf" <vvf@vvf.com> wrote in message
news:e1$cRYqPJHA.5080@TK2MSFTNGP03.phx.gbl...
for (int i = 0; i <= iUpperBound && (WaitForSingleObject(m_hQuitEvent,
0)
== WAIT_TIMEOUT); ++i)
One optimization I did not see yet mentioned is that you can replace the
WaitForSingleObject(m_hQuitEvent, 0)
with a simple bool variable, e.g.
for (int i = 0; i <= iUpperBound && !g_bQuit; ++i)
No need to use an event. Just set the global bool variable g_bQuit to
true
when you want the thread to quit.
That's not so much an optimization as it is an alternative technique. To
see what I mean, consider that the OP may be using the event elsewhere in
WFMO calls. If he is, he should continue using the event everywhere.
You crack me up. With all of your knowledge and precision, you don't see
reading a global variable is more efficient than calling a function?
Especially when WaitForSingleObject() relinquishes the quantum of the time
slice (well, with a timeout of 0, I think it still does); therefore, there
is no way to argue that replacing this code with a memory read is not an
optimization. The OP is not using the event anywhere else, and even if he
were, how does using it here justify the decreased efficiency?
That said, if you use the bool technique, you must declare it volatile.
For
anyone who doesn't appreciate why, see my discussion with Tommy in the
thread, "Should I use mutex in this context?" in
microsoft.public.vc.language, which was taking place just a couple of days
ago. See my reply (on 10/25) to the OP in that same thread for a good
overview of the various approaches to the problem and the issues and/or
advantages they have. In particular, event objects come in really handy
when you have other objects to wait on and need an escape valve for an
otherwise infinite wait.
Thanks, the thread is at
http://groups.google.com/group/microsoft.public.vc.language/browse_thread/thread/f092254ec9555669).
I'm not sure you proved the point that with VC2005 on x86 that there would
actually be a problem if you DIDN'T use volatile, but it's good to use
volatile just in case.
-- David