本文整理汇总了C++中boost::condition_variable::timed_wait方法的典型用法代码示例。如果您正苦于以下问题:C++ condition_variable::timed_wait方法的具体用法?C++ condition_variable::timed_wait怎么用?C++ condition_variable::timed_wait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boost::condition_variable
的用法示例。
在下文中一共展示了condition_variable::timed_wait方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: do_test
void do_test(void (this_type::*test_func)())
{
Lock lock(m);
locked=false;
done=false;
boost::thread t(test_func,this);
try
{
{
boost::mutex::scoped_lock lk(done_mutex);
BOOST_CHECK(!done_cond.timed_wait(lk, boost::posix_time::seconds(1),
boost::bind(&this_type::is_done,this)));
}
lock.unlock();
{
boost::mutex::scoped_lock lk(done_mutex);
BOOST_CHECK(done_cond.timed_wait(lk, boost::posix_time::seconds(1),
boost::bind(&this_type::is_done,this)));
}
t.join();
BOOST_CHECK(locked);
}
catch(...)
{
lock.unlock();
t.join();
throw;
}
}
示例2: main
int main()
{
#if defined(BOOST_THREAD_PLATFORM_PTHREAD)
for (int i=0; i<3; ++i)
{
const time_t now_time = ::time(0);
const time_t wait_time = now_time+1;
time_t end_time;
assert(now_time < wait_time);
boost::unique_lock<boost::mutex> lk(mtx);
//const bool res =
(void)cv.timed_wait(lk, from_time_t(wait_time));
end_time = ::time(0);
std::cerr << "now_time =" << now_time << " \n";
std::cerr << "end_time =" << end_time << " \n";
std::cerr << "wait_time=" << wait_time << " \n";
std::cerr << "now_time =" << from_time_t(now_time) << " \n";
std::cerr << "end_time =" << from_time_t(end_time) << " \n";
std::cerr << "wait_time=" << from_time_t(wait_time) << " \n";
std::cerr << end_time - wait_time << " \n";
assert(end_time >= wait_time);
std::cerr << " OK\n";
}
#endif
return 0;
}
示例3: operator
void operator()()
{
Lock lock(m);
typedef test_initially_unlocked_if_other_thread_has_lock<Mutex,Lock> this_type;
boost::thread t(&this_type::locking_thread,this);
try
{
{
boost::mutex::scoped_lock lk(done_mutex);
BOOST_CHECK(done_cond.timed_wait(lk,boost::posix_time::seconds(2),
boost::bind(&this_type::is_done,this)));
BOOST_CHECK(!locked);
}
lock.unlock();
t.join();
}
catch(...)
{
lock.unlock();
t.join();
throw;
}
}
示例4: relative_timed_wait_with_predicate
void relative_timed_wait_with_predicate()
{
boost::unique_lock<boost::mutex> lock(mutex);
if(cond_var.timed_wait(lock,boost::posix_time::seconds(timeout_seconds),check_flag(flag)) && flag)
{
++woken;
}
}
示例5: timedWaitOnMutex
bool timedWaitOnMutex(boost::condition_variable &waitCond, boost::mutex &waitMutex, int secs, int msecs)
{
boost::posix_time::ptime waitTime(boost::posix_time::microsec_clock::universal_time());
waitTime += boost::posix_time::seconds(secs);
waitTime += boost::posix_time::millisec(msecs);
boost::unique_lock<boost::mutex> lock(waitMutex);
return waitCond.timed_wait(lock, waitTime) == false;
}
示例6: timed_wait_with_predicate
void timed_wait_with_predicate()
{
boost::system_time const timeout=boost::get_system_time()+boost::posix_time::seconds(timeout_seconds);
boost::unique_lock<boost::mutex> lock(mutex);
if(cond_var.timed_wait(lock,timeout,check_flag(flag)) && flag)
{
++woken;
}
}
示例7: auto_flush
/*!
* The auto flusher ensures that buffers are force committed when
* the user has not called get_new() within a certain time window.
*/
void auto_flush(void)
{
boost::mutex::scoped_lock lock(_mutex);
const bool timeout = not _cond.timed_wait(lock, AUTOFLUSH_TIMEOUT);
if (timeout and _ok_to_auto_flush and _last_send_buff and _bytes_in_buffer != 0)
{
_last_send_buff->commit(_bytes_in_buffer);
_last_send_buff.reset();
}
}
示例8: do_transaction
void do_transaction(int comm_state) {
lock_t lock(mtx_);
comm_state_ = comm_state;
lock.unlock();
my_io_service_.send(tx_);
lock.lock();
while(comm_state_!=CST_IDLE){
if(!rx_cond_var_.timed_wait(lock, boost::posix_time::milliseconds(op_timeout_ms_))){
throw OperationTimeoutExcept();
}
}
}
示例9: timed_wait_and_pop
BOOLEAN timed_wait_and_pop ( Data& value, INT64 millsec )
{
boost::system_time const timeout=boost::get_system_time() +
boost::posix_time::milliseconds(millsec);
boost::mutex::scoped_lock lock ( _mutex ) ;
while ( _queue.empty () )
if ( !_cond.timed_wait ( lock, timeout ) )
return FALSE ;
value = _queue.front () ;
_queue.pop () ;
return TRUE ;
}
示例10: timed_wait_without_predicate
void timed_wait_without_predicate()
{
boost::system_time const timeout=boost::get_system_time()+boost::posix_time::seconds(timeout_seconds);
boost::unique_lock<boost::mutex> lock(mutex);
while(!flag)
{
if(!cond_var.timed_wait(lock,timeout))
{
return;
}
}
++woken;
}
示例11: timed_wait
bool timed_wait(Duration d)
{
boost::system_time const target=boost::get_system_time()+d;
boost::unique_lock<boost::mutex> l(m);
while(!flag)
{
if(!cond.timed_wait(l,target))
{
return flag;
}
}
return true;
}
示例12: wait
int wait(const boost::system_time& wait_until_abs_time, int* old_val = NULL) {
#if 0
if (old_val && *old_val != m_count) {
*old_val = m_count;
return 0;
}
#endif
scoped_lock g(m_lock);
try {
return m_cond.timed_wait(g, wait_until_abs_time) ? 0 : ETIMEDOUT;
} catch (std::exception&) {
return -1;
}
}
示例13: timed_lock_shared
bool timed_lock_shared(system_time const& timeout)
{
boost::this_thread::disable_interruption do_not_disturb;
boost::mutex::scoped_lock lk(state_change);
while(state.exclusive || state.exclusive_waiting_blocked)
{
if(!shared_cond.timed_wait(lk,timeout))
{
return false;
}
}
++state.shared_count;
return true;
}
示例14: TimerThreadProc
/**
* Worker thread proc for Timer objects.
*/
void Timer::TimerThreadProc(void)
{
Utility::SetThreadName("Timer Thread");
for (;;) {
boost::mutex::scoped_lock lock(l_Mutex);
typedef boost::multi_index::nth_index<TimerSet, 1>::type NextTimerView;
NextTimerView& idx = boost::get<1>(l_Timers);
/* Wait until there is at least one timer. */
while (idx.empty() && !l_StopThread)
l_CV.wait(lock);
if (l_StopThread)
break;
NextTimerView::iterator it = idx.begin();
Timer::Ptr timer = it->lock();
if (!timer) {
/* Remove the timer from the list if it's not alive anymore. */
idx.erase(it);
continue;
}
double wait = timer->m_Next - Utility::GetTime();
if (wait > 0) {
/* Make sure the timer we just examined can be destroyed while we're waiting. */
timer.reset();
/* Wait for the next timer. */
l_CV.timed_wait(lock, boost::posix_time::milliseconds(wait * 1000));
continue;
}
/* Remove the timer from the list so it doesn't get called again
* until the current call is completed. */
l_Timers.erase(timer);
lock.unlock();
/* Asynchronously call the timer. */
Utility::QueueAsyncCallback(boost::bind(&Timer::Call, timer));
}
}
示例15: wait_and_pop
bool wait_and_pop(T& data, boost::posix_time::microseconds waitTime)
{
boost::system_time timeout = boost::get_system_time() + waitTime;
boost::mutex::scoped_lock lockHandle(mutexHandle);
if (condFlag.timed_wait(lockHandle, timeout,
isEmpty(&queueHandle))) {
data = queueHandle.front();
queueHandle.pop();
condFlag.notify_all();
return true;
}
else {
return false;
}
}