Re: MT Design Question
On Aug 29, 10:02 am, "Chris M. Thomasson" <cris...@charter.net> wrote:
"Joshua Maurice" <joshuamaur...@gmail.com> wrote in message
news:c56c7bf9-86d4-44dc-a4fa-04ea55227325@s17g2000prh.googlegroups.com...
On Aug 28, 9:32 pm, "joe" <jc1...@att.net> wrote:
Joshua Maurice wrote:
On Aug 26, 1:35 pm, "joe" <jc1...@att.net> wrote:
See the Windows API WaitForMultipleObjects. This kind of stuff is
trivial in Windows.
This is sort of a pet peeve of mine, but please don't suggest to
anyone to actually directly use the windows threading API if at all
possible. It's borked, at least for windows XP and earlier, as it
lacks condition variables.
Well, ya see, me a programmer on Wintel has no need for UNIX/pthreads=
.. I
am not a computer scientist like you are.
Do you have any nontrivial
examples where windows event classes result in simpler and more
efficient code than the condition variable alternative?
An event can make some code simpler. Take a slow path for, say a simple
mutex algorithm:
<event version - pseudo-code sketch>
_________________________________________________________
struct mutex
{
LONG m_state; // = 0
HANDLE m_waitset; // auto reset event; set to false
void lock()
{
if (InterlockedExchange(&m_state, 1))
{
while (InterlockedExchange(&m_state, 2))
{
WaitForSingleObject(m_waitset, INFINITE);
}
}
}
void unlock()
{
if (InterlockedExchange(&m_state, 0) == 2)
{
SetEvent(m_waitset);
}
}};
Surely you mean:
void lock()
{
//while, not if
while (InterlockedExchange(&m_state, 1))
{
while (InterlockedExchange(&m_state, 2))
{
WaitForSingleObject(m_waitset, INFINITE);
}
}
}
You need to set the m_state to locked aka 1 after a wakeup. Right?
Also, this has the same comment as your other solutions. It works when
there are no spurious wakeups nor timed waits. I'm not sure if windows
allows or has spurious wakeups, but isn't INFINITE just a macro for a
very large number, and doesn't that result just in a (very long) timed
wait? Or do they have a special case hackery in the internals for that
very large number to actually mean infinite?