Add wxCondition::Wait() overload that also tests the condition.
Add Wait() overload that takes a functor argument and doesn't return until the condition is signaled _and_ the predicate returns true. This is useful for dealing with spurious wakeups and is modeled after C++11 std::condition_variable's corresponding method. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74771 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -344,6 +344,19 @@ public:
|
|||||||
// the lock on the associated mutex object, before returning.
|
// the lock on the associated mutex object, before returning.
|
||||||
wxCondError Wait();
|
wxCondError Wait();
|
||||||
|
|
||||||
|
// std::condition_variable-like variant that evaluates the associated condition
|
||||||
|
template<typename Functor>
|
||||||
|
wxCondError Wait(const Functor& predicate)
|
||||||
|
{
|
||||||
|
while ( !predicate() )
|
||||||
|
{
|
||||||
|
wxCondError e = Wait();
|
||||||
|
if ( e != wxCOND_NO_ERROR )
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
return wxCOND_NO_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
// exactly as Wait() except that it may also return if the specified
|
// exactly as Wait() except that it may also return if the specified
|
||||||
// timeout elapses even if the condition hasn't been signalled: in this
|
// timeout elapses even if the condition hasn't been signalled: in this
|
||||||
// case, the return value is wxCOND_TIMEOUT, otherwise (i.e. in case of a
|
// case, the return value is wxCOND_TIMEOUT, otherwise (i.e. in case of a
|
||||||
|
@@ -169,6 +169,33 @@ public:
|
|||||||
*/
|
*/
|
||||||
wxCondError Wait();
|
wxCondError Wait();
|
||||||
|
|
||||||
|
/**
|
||||||
|
Waits until the condition is signalled and the associated condition true.
|
||||||
|
|
||||||
|
This is a convenience overload that may be used to ignore spurious
|
||||||
|
awakenings while waiting for a specific condition to become true.
|
||||||
|
|
||||||
|
Equivalent to
|
||||||
|
@code
|
||||||
|
while ( !predicate() )
|
||||||
|
{
|
||||||
|
wxCondError e = Wait();
|
||||||
|
if ( e != wxCOND_NO_ERROR )
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
return wxCOND_NO_ERROR;
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
The predicate would typically be a C++11 lambda:
|
||||||
|
@code
|
||||||
|
condvar.Wait([]{return value == 1;});
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
@since 3.0
|
||||||
|
*/
|
||||||
|
template<typename Functor>
|
||||||
|
wxCondError Wait(const Functor& predicate);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Waits until the condition is signalled or the timeout has elapsed.
|
Waits until the condition is signalled or the timeout has elapsed.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user