Re: MT Design Question
On Aug 26, 9:35 pm, "joe" <jc1...@att.net> wrote:
Scott Meyers wrote:
This is a threading-related design question assuming that
C++0x is the implementation language.
On what platform?
If he's asking about C++0x, he's asking about a more or less
portable solution (a solution portable to all platforms which
support C++0x). The platform shouldn't matter.
Suppose I have a graph that I want to search for a node that
has some characteristic (e.g., holds a value within some
range). Suppose further that of the many ways to search the
graph (e.g., depth-first, breadth-first, random walk, etc.),
I can't predict which will be best, so I want to run them
all concurrently, stopping when either one of them finds a
suitable node or they all fail. That is, given something
like this
Node* dfsSearch(Graph g, Predicate p); // do dfs search of g for
// a node satisfying p
Node* bfsSearch(Graph g, Predicate p); // do bfs search of g
Node* rwSearch(Graph g, Predicate p); // do random walk search
of g
I want to do this:
concurrently invoke dfsSearch, bfsSearch, and rwSearch;
wait until one returns success or they all return lack of success;
if one returned success, tell the others to stop searching;
One of the problems in mapping this to C++0x is that C++0x
has no notion of thread cancellation/interruption, but that
can be built manually, as Anthony Williams shows in section
9.2 of his "C++ Concurrency in Action"
(http://www.manning.com/williams/), which I'm pleased to be
able to plug here.
My question, however, has to do with implementing this part:
wait until one returns success or they all return lack of success;
See the Windows API WaitForMultipleObjects. This kind of stuff
is trivial in Windows.
At least in Windows XP, WaitForMultipleObjects did NOT know how
to wait for a C++0x future. (Understandably so, since the
proposal for futures hadn't even been drafted when Windows XP
came out.) Scott mentionned one possible C++0x solution, using
condition variables, which are considerably more flexible than
WaitForMultipleObjects (although in simple cases,
WaitForMultipleObjects may be easier to use). And is at least
somewhat portable, even today, since Boost::thread supports
condition variables for both Windows and Unix. (They're the
standard idiom in Unix, supported directly by pthreads. And I
believe that Windows 7 supports them directly as well---under
earlier versions they have to be emulated.)
--
James Kanze