重置线程的休眠时间

时间:2014-03-10 05:42:02

标签: c++ multithreading boost boost-thread

假设有一个像这样的线程

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

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

最好的方法是什么?

2 个答案:

答案 0 :(得分:1)

使用condition_variable表示对截止日期的更改

这有利于支持超时缩短

的情况。

查看 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标准库(无信令)

这是一种仅限标准库的方法,在截止日期前不使用同步。

我更倾向于使用原子time_point作为截止日期值本身,但这不受支持。接下来最好的事情是shared_ptr<time_point>std::atomic_load / atomic_store)但我的编译器库尚未实现(grrr)。

所以,相反,我从开始时间开始共享'偏移':

#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();
}

查看 Live on Coliru

答案 1 :(得分:0)

这是一个快速而肮脏的方法:

volatile bool someCondition = false;

void callFromOtherThread(bool x) {
  boost::lock_guard<boost::mutex> lock(mylock2);
  someCondition = x;
}

void mythread()
{
  int res;
  while(1) {
    bool keepwaiting = false;
    {
      boost::lock_guard<boost::mutex> lock(mylock2);
      keepwaiting = someCondition;
    }
    if (!keepwaiting) {
       boost::lock_guard<boost::mutex> lock(mylock);
       res = do_my_stuff();
    }
    boost::this_thread::sleep(boost::posix_time::seconds(5));
  }
}

当你的线程完成睡眠时,会检查它发生了“其他事情”,如果发生了,它会跳过'do_my_stuff()'并再次回到睡眠状态。

我怀疑有关您的用例的更多信息,可能有可能重写事物以使用条件变量。