本文整理汇总了C++中std::condition_variable::wait_for方法的典型用法代码示例。如果您正苦于以下问题:C++ condition_variable::wait_for方法的具体用法?C++ condition_variable::wait_for怎么用?C++ condition_variable::wait_for使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std::condition_variable
的用法示例。
在下文中一共展示了condition_variable::wait_for方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: resized
void resized(const boo::SWindowRect& rect)
{
std::unique_lock<std::mutex> lk(m_mt);
m_latestResize = rect;
m_hasResize = true;
m_resizeCv.wait_for(lk, std::chrono::milliseconds(500));
}
示例2: push
/**
* Push an element onto the queue. If the queue has a max size,
* this call will block if the queue is full.
*/
void push(T value) {
constexpr const std::chrono::milliseconds max_wait{10};
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
++m_push_counter;
#endif
if (m_max_size) {
while (size() >= m_max_size) {
std::unique_lock<std::mutex> lock{m_mutex};
m_space_available.wait_for(lock, max_wait, [this] {
return m_queue.size() < m_max_size;
});
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
++m_full_counter;
#endif
}
}
std::lock_guard<std::mutex> lock{m_mutex};
m_queue.push(std::move(value));
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
if (m_largest_size < m_queue.size()) {
m_largest_size = m_queue.size();
}
#endif
m_data_available.notify_one();
}
示例3: testDocCount
TestResult testDocCount()
{
_messageReceived.clear();
const std::string queryMessage = "active_docs_count";
_adminWs->sendFrame(queryMessage.data(), queryMessage.size());
std::unique_lock<std::mutex> lock(_messageReceivedMutex);
if (_messageReceived.empty() &&
_messageReceivedCV.wait_for(lock, std::chrono::milliseconds(_messageTimeoutMilliSeconds)) == std::cv_status::timeout)
{
Log::info("testDocCount: Timed out waiting for admin console message");
return TestResult::TEST_TIMED_OUT;
}
lock.unlock();
StringTokenizer tokens(_messageReceived, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
if (tokens.count() != 2 ||
tokens[0] != "active_docs_count" ||
std::stoi(tokens[1]) != _docsCount)
{
Log::info("testDocCount: Unrecognized message format");
return TestResult::TEST_FAILED;
}
else if (std::stoi(tokens[1]) != _docsCount)
{
Log::info("testDocCount: Incorrect doc count "
", expected: " + std::to_string(_docsCount) +
", actual: " + tokens[1]);
return TestResult::TEST_FAILED;
}
Log::info(std::string("testDocCount: OK"));
return TestResult::TEST_OK;
}
示例4: testRmDocNotify
TestResult testRmDocNotify()
{
_messageReceived.clear();
// subscribe to rmdoc notification on admin websocket
const std::string subscribeMessage = "subscribe rmdoc";
_adminWs->sendFrame(subscribeMessage.data(), subscribeMessage.size());
_docWs1->close();
std::unique_lock<std::mutex> lock(_messageReceivedMutex);
if (_messageReceived.empty() &&
_messageReceivedCV.wait_for(lock, std::chrono::milliseconds(_messageTimeoutMilliSeconds)) == std::cv_status::timeout)
{
Log::info("testRmDocNotify: Timed out waiting for admin console message");
return TestResult::TEST_TIMED_OUT;
}
lock.unlock();
StringTokenizer tokens(_messageReceived, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
if (tokens.count() != 3 ||
tokens[0] != "rmdoc" ||
stoi(tokens[1]) != _docPid1)
{
Log::info("testRmDocNotify: Invalid message format");
return TestResult::TEST_FAILED;
}
_usersCount--;
Log::info(std::string("testRmDocNotify: OK"));
return TestResult::TEST_OK;
}
示例5: setupAbortChecker
std::shared_ptr<WdtAbortChecker> setupAbortChecker() {
int abortSeconds = FLAGS_abort_after_seconds;
if (abortSeconds <= 0) {
return nullptr;
}
LOG(INFO) << "Setting up abort " << abortSeconds << " seconds.";
static std::atomic<bool> abortTrigger{false};
auto res = std::make_shared<WdtAbortChecker>(abortTrigger);
auto lambda = [=] {
LOG(INFO) << "Will abort in " << abortSeconds << " seconds.";
std::unique_lock<std::mutex> lk(abortMutex);
bool isNotAbort =
abortCondVar.wait_for(lk, std::chrono::seconds(abortSeconds),
[&]() -> bool { return isAbortCancelled; });
if (isNotAbort) {
LOG(INFO) << "Already finished normally, no abort.";
} else {
LOG(INFO) << "Requesting abort.";
abortTrigger.store(true);
}
};
// Run this in a separate thread concurrently with sender/receiver
static auto f = std::async(std::launch::async, lambda);
return res;
}
示例6: timedWaitOnMutex
//! Returns true if timeout hit
bool timedWaitOnMutex(std::condition_variable &waitCond, std::mutex &waitMutex, int secs, int msecs)
{
auto span = std::chrono::seconds(secs) + std::chrono::milliseconds(msecs);
std::unique_lock<std::mutex> lock(waitMutex);
return waitCond.wait_for(lock, span) == std::cv_status::timeout;
}
示例7: run
void run() {
std::unique_lock<std::mutex>lock(mutex_);
while (!done_) {
cond_.wait_for(lock, std::chrono::milliseconds(interval_));
print();
}
}
示例8: move
boost::optional<Json::Value>
findMsg(std::chrono::milliseconds const& timeout,
std::function<bool(Json::Value const&)> pred) override
{
std::shared_ptr<msg> m;
{
std::unique_lock<std::mutex> lock(m_);
if(! cv_.wait_for(lock, timeout,
[&]
{
for (auto it = msgs_.begin();
it != msgs_.end(); ++it)
{
if (pred((*it)->jv))
{
m = std::move(*it);
msgs_.erase(it);
return true;
}
}
return false;
}))
{
return boost::none;
}
}
return std::move(m->jv);
}
示例9: exitLock
/**
* This case tests if multiple requests are being sent up to AVS with a reset during the process.
*/
TEST_F(UserInactivityMonitorTest, sendMultipleReportsWithReset) {
InSequence s;
std::mutex exitMutex;
std::unique_lock<std::mutex> exitLock(exitMutex);
int repetitionCount = 5;
EXPECT_CALL(*m_mockMessageSender, sendMessage(ResultOf(&checkMessageRequest, Eq(true))))
.Times(AtLeast(repetitionCount - 1));
EXPECT_CALL(*m_mockMessageSender, sendMessage(ResultOf(&checkMessageRequestAndReleaseTrigger, Eq(true)))).Times(1);
auto userInactivityMonitor = UserInactivityMonitor::create(
m_mockMessageSender, m_mockExceptionEncounteredSender, USER_INACTIVITY_REPORT_PERIOD);
ASSERT_NE(nullptr, userInactivityMonitor);
auto directiveSequencer = adsl::DirectiveSequencer::create(m_mockExceptionEncounteredSender);
directiveSequencer->addDirectiveHandler(userInactivityMonitor);
auto userInactivityDirectiveHeader = std::make_shared<AVSMessageHeader>(
USER_INACTIVITY_RESET_NAMESPACE, USER_INACTIVITY_RESET_NAME, USER_INACTIVITY_MESSAGE_ID);
auto attachmentManager = std::make_shared<StrictMock<attachment::test::MockAttachmentManager>>();
std::shared_ptr<AVSDirective> userInactivityDirective =
AVSDirective::create("", userInactivityDirectiveHeader, "", attachmentManager, "");
std::this_thread::sleep_for(2 * USER_INACTIVITY_REPORT_PERIOD + USER_INACTIVITY_REPORT_PERIOD / 2);
directiveSequencer->onDirective(userInactivityDirective);
exitTrigger.wait_for(exitLock, repetitionCount * USER_INACTIVITY_REPORT_PERIOD + USER_INACTIVITY_REPORT_PERIOD / 2);
directiveSequencer->shutdown();
}
示例10: waitForUpdate
void waitForUpdate(std::mutex& m, std::condition_variable& cv, bool& x) {
std::unique_lock<std::mutex> lk(m);
if (!x) {
cv.wait_for(lk, std::chrono::seconds(5), [&] { return x; });
}
ASSERT_TRUE(x);
x = false;
}
示例11: waits
void waits(int idx)
{
std::unique_lock<std::mutex> lk(cv_m);
if(cv.wait_for(lk, std::chrono::milliseconds(idx*100), [](){return i == 1;}))
std::cerr << "Thread " << idx << " finished waiting. i == " << i << '\n';
else
std::cerr << "Thread " << idx << " timed out. i == " << i << '\n';
}
示例12: wait
bool wait(int seconds) {
std::unique_lock<std::mutex> lk(m);
cv.wait_for(lk, std::chrono::seconds(seconds),
std::bind(&cracker::isFound, this));
lk.unlock();
return found;
}
示例13: task1
void task1() {
log("Starting task 1. Waiting on cv for 2 secs.");
lock_type lck(mtx);
bool done = cv.wait_for(lck, 2*sec, [] {return ready;});
std::stringstream ss;
ss << "Task 1 finished, done==" << (done?"true":"false") << ", " << (lck.owns_lock()?"lock owned":"lock not owned");
log(ss.str());
}
示例14: interruptible_wait
void interruptible_wait(std::condition_variable& cv, std::unique_lock<std::mutex>& lk)
{
interruption_point();
this_thread_interrupt_flag.set_condition_variable(cv);
clear_cv_on_destruct guard;
interruption_point();
cv.wait_for(lk, std::chrono::milliseconds(1));
interruption_point();
}
示例15: lock
bool
wait_for(
const std::chrono::duration<Rep, Period>& time)
{
if (!flag.load(std::memory_order_consume)) {
std::unique_lock<std::mutex> lock(mutex);
return condition.wait_for(lock, time, std::bind(&request_t<Work, Result>::ready, this));
}
}