Re: Threading in new C++ standard
On Apr 16, 12:28 am, "Dann Corbit" <dcor...@connx.com> wrote:
Rather than create a new way of doing things:http://www.open-std.org/jtc1/=
sc22/wg21/docs/papers/2008/n2497.html
...
I like that some higher level construction is also there at last (at
condition_variable):
<quote>
template <class Predicate>
void wait(unique_lock<mutex>& lock, Predicate pred);
Effects:
As if:
while (!pred())
wait(lock);
</quote>
Now programming the get operation of a bounded buffer would look
something like this, I guess:
queue BB::q;
mutex BB::m;
condition_variable BB::empty;
condition_variable BB::full;
int BB::get()
{
int b;
unique_lock<mutex> lock(m);
full.wait(lock, !q.empty());
b = q.get();
empty.notify_one();
return b;
}
Although there is still some duplication in `full.wait(lock, !
q.empty());', it is much closer to this simple pseudo code (where
`when' is a keyword for a conditional critical region statement) than
a pthread version:
shared queue BB::q;
int BB::get()
{
int b;
when (!q.empty()) {
b = q.get();
}
return b;
}
Best Regards,
Szabolcs