|
Boost : |
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2008-05-19 16:11:12
Hello,
In the Join library I have see the following idiom
mutex mtx;
condition_variable cond;
unsigned num_blocked;
void foo() {
unique_lock l(mtx);
++num_blocked;
cond.wait(lock);
--num_blocked;
// ...
}
void foo() {
unique_lock l(mtx);
// ...
if (num_blocked > 0) cond.notify_one();
// ...
}
Does this perform better in some contexts? if yes in which ones?
If yes, does this justify a condition wrapper like:
template <
class Condition
>
class condition_booster{
public:
typedef Condition condition;
void notify_one() { if (num_blocked > 0) cond.notify_one(); }
void notify_all() { if (num_blocked > 0) cond.notify_all(); }
template <typename Locker, typename Predicate>
template <typename Locker>
void wait(Locker& lock) {
++num_blocked;
cond.wait(lock);
--num_blocked;
}
void wait(Locker& lock, Predicate& pred) {
while(!pred()) wait(lock);
}
// ...
private:
Condition cond;
unsigned num_blocked;
};
Best
_____________________
Vicente Juan Botet Escriba
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk