重置线程的睡眠时间 [英] Resetting sleeping time of a thread

查看:63
本文介绍了重置线程的睡眠时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个这样的线程

void mythread()
{
  int res;
  while(1) {
    {
       boost::lock_guard<boost::mutex> lock(mylock);
       res = do_my_stuff();
    }
    boost::this_thread::sleep(boost::posix_time::seconds(5));
  }
}

并且该线程当前正在休眠.如果线程外发生了某些事情,我希望能够增加睡眠时间.

and that the thread is currently sleeping. If something happens outside of the thread, I'd like to be able to increase the sleep time.

最好的方法是什么?

推荐答案

使用 condition_variable 表示更改截止日期

这样做的好处是支持缩短缩短的情况:

Using a condition_variable to signal changes to the deadline

This has the benefit of supporting scenarios where the timeout is shortened:

查看 在Coliru上直播

See it Live On Coliru

#include <thread>
#include <chrono>
#include <iostream>
#include <condition_variable>

namespace demo
{
    namespace chrono = std::chrono;

    using our_clock = chrono::system_clock;

    struct Worker
    {
        mutable std::mutex _mx;

        // shared, protected by _mx:
        our_clock::time_point _deadline; 
        mutable std::condition_variable _cv;

        Worker(our_clock::time_point deadline) : _deadline(deadline) {}

        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";
        }
    };
}

int main()
{
    using namespace demo;

    Worker worker(our_clock::now() + chrono::seconds(2));
    auto th = std::thread(std::cref(worker));

    // after 2 seconds, update the timepoint
    std::this_thread::sleep_for(chrono::seconds(1));

    {
        std::lock_guard<std::mutex> lk(worker._mx);
        std::cout << "Updating shared delay value..." << "\n";

        worker._deadline = our_clock::now() + chrono::seconds(1);
        worker._cv.notify_all();
    }

    th.join();
}

C ++ 11标准库(无信令)

这是仅适用于标准库的方法,在截止日期前不使用同步.

C++11 standard library (no signaling)

Here's a standard-library only approach which uses no synchronization around the deadline.

我宁愿使用原子性的 time_point 作为截止日期值本身,但是不支持.下一个最好的方法是 shared_ptr< time_point> (带有 std :: atomic_load / atomic_store ),但是我的编译器的库尚未实现( grrr ).

I'd have preferred to use atomic time_point for the deadline value itself, but that's not supported. Next best thing would have been shared_ptr<time_point> (with std::atomic_load/atomic_store) but my compiler's library doesn't implement this yet (grrr).

因此,相反,我从开始时间起就共享偏移":

So, instead, I share the 'offset' since a start time:

#include <thread>
#include <chrono>
#include <iostream>
#include <atomic>

namespace demo
{
    namespace chrono = std::chrono;

    using our_clock = chrono::system_clock;
    using shared_delay = std::atomic<our_clock::duration>;

    void worker(our_clock::time_point const start, shared_delay const& delay)
    {
        for (our_clock::time_point deadline; our_clock::now() < (deadline = start + delay.load());)
        {
            std::cout << "worker: Sleeping for " << chrono::duration_cast<chrono::milliseconds>(deadline - our_clock::now()).count() << "ms...\n";
            std::this_thread::sleep_until(deadline);
        }

        std::cout << "worker: Done\n";
    }
}

int main()
{
    using namespace demo;

    shared_delay delay(chrono::seconds(2));
    auto th = std::thread(worker, our_clock::now(), std::cref(delay));

    // after 2 seconds, update the timepoint
    std::this_thread::sleep_for(chrono::seconds(1));
    std::cout << "Updating shared delay value..." << "\n";
    delay.store(chrono::seconds(3));

    th.join();
}

查看 在Coliru上直播

See it Live on Coliru

这篇关于重置线程的睡眠时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆