当前位置: 首页>>代码示例>>C++>>正文


C++ condition_variable::wait_until方法代码示例

本文整理汇总了C++中std::condition_variable::wait_until方法的典型用法代码示例。如果您正苦于以下问题:C++ condition_variable::wait_until方法的具体用法?C++ condition_variable::wait_until怎么用?C++ condition_variable::wait_until使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在std::condition_variable的用法示例。


在下文中一共展示了condition_variable::wait_until方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: ThreadLoop

 void ThreadLoop() {
   while (go_on) {
     {
       std::unique_lock < std::mutex > lock(mutex);
       auto now = std::chrono::system_clock::now();
       auto cur = waiting_tasks.begin();
       // Run a runnable task
       if (waiting_tasks.empty() == false && (*cur).getTime() <= now
           && (*cur).isCancelled() == false) {
         auto ins = running_tasks.insert(*cur);
         waiting_tasks.erase(*cur);
         cur = ins.first;
         (*cur).go();  // Callback will be launched in a detached thread
         running_tasks.erase(*cur);
       }
       // waiting for the next event
       if (waiting_tasks.empty()) {
         blocker.wait(lock);
       } else {
         /*
          * 	Bad: Valgrind complains "invalid read of size 8"
          * 	http://stackoverflow.com/questions/9891767/valgrind-debug-log-invalid-read-of-size-8
          *
          blocker.wait_until(lock, (*cur).getTime());
          *
          */
         auto nxt = waiting_tasks.begin();
         auto nxttime = (*nxt).getTime();
         blocker.wait_until(lock, nxttime);
       }
     }
   }
 }
开发者ID:danieleatgithub,项目名称:Homer,代码行数:33,代码来源:Scheduler.hpp

示例2: timedwait_monotonic

void timedwait_monotonic(int id)
{
    std::unique_lock<std::mutex> lock(s_mutex);

    //
    // http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until
    //
    // The clock tied to timeout_time is used, which is not required to be a 
    // monotonic clock.There are no guarantees regarding the behavior of this
    // function if the clock is adjusted discontinuously, but the existing
    // implementations convert timeout_time from Clock to std::chrono::system_clock
    // and delegate to POSIX pthread_cond_timedwait so that the wait honors 
    // ajustments to the system clock, but not to the the user-provided Clock. 
    // In any case, the function also may wait for longer than until after 
    // timeout_time has been reached due to scheduling or resource contention delays.
    //
    auto start = std::chrono::steady_clock::now();
    std::cv_status ret = s_cond.wait_until(lock, start + std::chrono::seconds(5));

    auto end = std::chrono::steady_clock::now();
    std::chrono::duration<double> duration = end - start;

    if (ret == std::cv_status::timeout)
    {
        std::cout << "t" << id << " timeout: " << duration.count() << std::endl;
    }
    else
    {
        std::cout << "t" << id << " success: " << duration.count() << std::endl;
    }
}
开发者ID:kzfm1024,项目名称:misc,代码行数:31,代码来源:std_condition_variable_test.cpp

示例3: run

        void run() {
            {
                std::lock_guard<std::mutex> lk(mutex);
                if (running) return;
                running = true;
                for (auto& t : threads) {
                    t = std::thread([this]{this->loop();});
                }
            }
            while (true) {
                std::unique_lock<std::mutex> lk(mutex);
                if (!running) break;

                auto task_it = min_element(tasks.begin(), tasks.end());
                time_point next_task = task_it == tasks.end() ? clock_type::time_point::max() : task_it->start;
                if (tasks_updated.wait_until(lk, next_task) == std::cv_status::timeout) {
                    if (task_it->repeat != clock_type::duration::zero()) {
                        task_it->start += task_it->repeat;
                    }
                    else {
                        handles.remove(task_it);
                        tasks.erase(task_it);
                    }
                    todo.push_back(task_it);
                    modified.notify_all();
                }
            }
            for (auto& t : threads) {
                t.join();
            }
        }
开发者ID:OpsRaven,项目名称:SenecaOOP345-attic,代码行数:31,代码来源:thread-schedule.cpp

示例4: waits

void waits(int idx)
{
    std::unique_lock<std::mutex> lk(cv_m);
    auto now = std::chrono::system_clock::now();
    if(cv.wait_until(lk, now + 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';
}
开发者ID:WhiZTiM,项目名称:coliru,代码行数:9,代码来源:main.cpp

示例5: wait_loop

bool wait_loop() {
    const auto timeout =
        std::chrono::steady_clock::now() + std::chrono::milliseconds(500);
    std::unique_lock<std::mutex> lk(mtx);
    while (!done) {
        if (cv.wait_until(lk, timeout) == std::cv_status::timeout)
            break;
    }
    return done;
}
开发者ID:condy0919,项目名称:experimentations,代码行数:10,代码来源:cv_time.cpp

示例6: getReply

 SearchReply::UP getReply(uint32_t millis) {
     std::unique_lock<std::mutex> guard(_lock);
     auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(millis);
     while (!_reply) {
         if (_cond.wait_until(guard, deadline) == std::cv_status::timeout) {
             break;
         }
     }
     return std::move(_reply);
 }
开发者ID:songhtdo,项目名称:vespa,代码行数:10,代码来源:matchengine.cpp

示例7: fakeSocketPoll

int fakeSocketPoll(struct pollfd *pollfds, int nfds, int timeout)
{
    loggingBuffer << "FakeSocket Poll ";
    for (int i = 0; i < nfds; i++)
    {
        if (i > 0)
            loggingBuffer << ",";
        loggingBuffer << "#" << pollfds[i].fd << ":" << pollBits(pollfds[i].events);
    }
    loggingBuffer << ", timeout:" << timeout << flush();

    std::vector<FakeSocketPair>& fds = getFds();
    std::unique_lock<std::mutex> lock(theMutex);

    if (timeout > 0)
    {
        auto const now = std::chrono::steady_clock::now();
        auto const end = now + std::chrono::milliseconds(timeout);

        while (!checkForPoll(fds, pollfds, nfds))
            if (theCV.wait_until(lock, end) == std::cv_status::timeout)
            {
                loggingBuffer << "FakeSocket Poll timeout: 0" << flush();
                return 0;
            }
    }
    else if (timeout == 0)
    {
        checkForPoll(fds, pollfds, nfds);
    }
    else // timeout < 0
    {
        while (!checkForPoll(fds, pollfds, nfds))
            theCV.wait(lock);
    }

    int result = 0;
    for (int i = 0; i < nfds; i++)
    {
        if (pollfds[i].revents != 0)
            result++;
    }

    loggingBuffer << "FakeSocket Poll result: ";
    for (int i = 0; i < nfds; i++)
    {
        if (i > 0)
            loggingBuffer << ",";
        loggingBuffer << "#" << pollfds[i].fd << ":" << pollBits(pollfds[i].revents);
    }
    loggingBuffer << ": " << result << flush();

    return result;
}
开发者ID:LibreOffice,项目名称:online,代码行数:54,代码来源:FakeSocket.cpp

示例8: wait_until

 std::cv_status wait_until(const std::chrono::duration<Rep, Period>& abs_time)
 {
     auto lc = capo::make<std::unique_lock>(lock_);
     while (counter_ <= 0)
     {
         if (cond_.wait_until(lc, abs_time) == std::cv_status::timeout)
             return std::cv_status::timeout;
     }
     -- counter_;
     return std::cv_status::no_timeout;
 }
开发者ID:lllhhhqqq,项目名称:capo,代码行数:11,代码来源:semaphore.hpp

示例9: wait_until

 std::cv_status wait_until(const std::chrono::duration<Rep, Period>& abs_time)
 {
     auto lc = capo::make<std::unique_lock>(lock_);
     while (signaled_ == waiter_status::resting)
     {
         if (cond_.wait_until(lc, abs_time) == std::cv_status::timeout)
             return std::cv_status::timeout;
     }
     if (signaled_ == waiter_status::arrived)
         signaled_ = waiter_status::resting;
     return std::cv_status::no_timeout;
 }
开发者ID:Ricardo666666,项目名称:capo,代码行数:12,代码来源:waiter.hpp

示例10: operator

 void operator()() const {
     std::unique_lock<std::mutex> lk(_mx);
     _cv.wait_until(lk, _deadline, [this] 
             { 
                 std::cout << "worker: Signaled\n";
                 auto now = our_clock::now();
                 if (now >= _deadline)
                     return true;
                 std::cout << "worker: Still waiting " << chrono::duration_cast<chrono::milliseconds>(_deadline - now).count() << "ms...\n"; 
                 return false;
             });
     std::cout << "worker: Done\n";
 }
开发者ID:CCJY,项目名称:coliru,代码行数:13,代码来源:main.cpp

示例11: wait_ready

	bool fiber_waiter::wait_ready(std::chrono::steady_clock::time_point timeout_point) noexcept
	{
		if (gth_thread_type == thread_type::thread)
		{
			std::unique_lock<std::mutex> lk(m_thread_mutex);
			return m_thread_var.wait_until(lk, timeout_point, [this] { return m_ready.load(std::memory_order_relaxed); });
		}
		else
		{
			std::unique_lock<boost::fibers::mutex> lk(m_fiber_mutex);
			return m_fiber_var.wait_until(lk, timeout_point, [this] { return m_ready.load(std::memory_order_relaxed); });
		}
	}
开发者ID:dmlys,项目名称:testdev-extlib,代码行数:13,代码来源:future-fiber.cpp

示例12: g

 int wait
 (
     const std::chrono::time_point<Clock, Duration>& wait_until_abs_time,
     long* old_val = NULL
 ) {
     if (old_val) {
         long cur_val = m_count.load(std::memory_order_relaxed);
         if (*old_val != cur_val) {
             *old_val = cur_val;
             return 0;
         }
     }
     std::unique_lock<std::mutex> g(m_lock);
     return m_cond.wait_until(g, wait_until_abs_time) == std::cv_status::no_timeout
          ? 0 : ETIMEDOUT;
 }
开发者ID:saleyn,项目名称:utxx,代码行数:16,代码来源:synch.hpp

示例13: timedwait

void timedwait(int id)
{
    std::unique_lock<std::mutex> lock(s_mutex);

    auto start = std::chrono::system_clock::now();
    std::cv_status ret = s_cond.wait_until(lock, start + std::chrono::seconds(5));

    auto end = std::chrono::system_clock::now();
    std::chrono::duration<double> delta = end - start;

    if (ret == std::cv_status::timeout)
    {
        std::cout << "t" << id << " timeout: " << delta.count() << std::endl;
    }
    else
    {
        std::cout << "t" << id << " success: " << delta.count() << std::endl;
    }
}
开发者ID:kzfm1024,项目名称:misc,代码行数:19,代码来源:std_condition_variable_test.cpp

示例14: timer_thread

void timer_thread()
{
    while (!isCancelled) {
        std::unique_lock<std::mutex> lock(m);
        std::chrono::system_clock::time_point now;
        while (keys.empty() || earliest > (now = std::chrono::system_clock::now())) {
            if (keys.empty()) {
                cv.wait(lock);
            } else {
                cv.wait_until(lock, earliest);
            }

            if (isCancelled)
                return;
        }

        std::vector<int> expired = remove_expired(now);
        earliest = find_next_timeout();
        lock.unlock();
        std::for_each(expired.begin(), expired.end(), callback);
    }
}
开发者ID:CCJY,项目名称:coliru,代码行数:22,代码来源:main.cpp

示例15: main

int main() {
    /* thread */
    std::thread t1(factorial, 6);
    std::this_thread::sleep_for(chrono::milliseconds(3));
    chrono::steady_clock::time_point tp = chrono::steady_clock::now() + chrono::microseconds(4);
    std::this_thread::sleep_until(tp);

    /* Mutex */
    std::mutex mu;
    std::lock_guard<mutex> locker(mu);
    std::unique_lock<mutex> ulocker(mu);
    ulocker.try_lock();
    ulocker.try_lock_for(chrono::nanoseconds(500));
    ulocker.try_lock_until(tp);

    /* Condition Variable */
    std:condition_variable cond;
    cond.wait_for(ulocker, chrono::microseconds(2));
    cond.wait_until(ulocker, tp);

    /* Future and Promise */
    std::promise<int> p; 
    std::future<int> f = p.get_future();
    f.get();
    f.wait();
    f.wait_for(chrono::milliseconds(2));
    f.wait_until(tp);

    /* async() */
    std::future<int> fu = async(factorial, 6);

    /* Packaged Task */
    std::packaged_task<int(int)> t(factorial);
    std::future<int> fu2 = t.get_future();
    t(6);
 	
	 return 0;
}
开发者ID:MiichaelD,项目名称:c_cpp,代码行数:38,代码来源:threads_ch4.cpp


注:本文中的std::condition_variable::wait_until方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。