为啥std:chrono:steady

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为啥std:chrono:steady相关的知识,希望对你有一定的参考价值。

参考技术A 做什么,我认为是你想达到什么简单的。你的类later用下面的 INT(毫秒等到运行的代码) 布尔(如果为true则返回立即和运行在另一个线程指定后的代码) 变量(你会喂正是为std ::绑定) 您可以更改std::chrono::milliseconds至std::chrono::nanoseconds或microseconds对于更高的精度和添加第二个int和一个for循环来指定多少来运行代码。 在这里,你去,可享受:
#include <functional>
#include <chrono>
#include <future>
#include <cstdio>

class later

public:
template <class callable, class arguments>
later(int after, bool async, callable f, arguments args)

std::function<typename std::result_of<callable(arguments)>::type()> task(std::bind(std::forward<callable>(f), std::forward<arguments>(args)));

if (async)

std::thread([after, task]()
std::this_thread::sleep_for(std::chrono::milliseconds(after));
task();
).detach();

else

std::this_thread::sleep_for(std::chrono::milliseconds(after));
task();



;

void test1(void)

return;


void test2(int a)

printf("%i\n", a);
return;


int main()

later later_test1(1000, false, test1);
later later_test2(1000, false, test2, 101);

return 0;


两秒钟后输出:
101

2. 这是我的代码至今: 我用VC ++2012(无可变参数模板)
//header
#include <thread>
#include <mutex>
#include <condition_variable>
#include <vector>
#include <chrono>
#include <memory>
#include <algorithm>

template<class T>
class TimerThread

typedef std::chrono::high_resolution_clock clock_t;

struct TimerInfo

clock_t::time_point m_TimePoint;
T m_User;

template <class TArg1>
TimerInfo(clock_t::time_point tp, TArg1 arg1)
: m_TimePoint(tp)
, m_User(std::forward<TArg1>(arg1))



template <class TArg1, class TArg2>
TimerInfo(clock_t::time_point tp, TArg1 arg1, TArg2 arg2)
: m_TimePoint(tp)
, m_User(std::forward<TArg1>(arg1), std::forward<TArg2>(arg2))


;

std::unique_ptr<std::thread> m_Thread;
std::vector<TimerInfo> m_Timers;
std::mutex m_Mutex;
std::condition_variable m_Condition;
bool m_Sort;
bool m_Stop;

void TimerLoop()

for (;;)

std::unique_lock<std::mutex> lock(m_Mutex);

while (!m_Stop m_Timers.empty())

m_Condition.wait(lock);


if (m_Stop)

return;


if (m_Sort)

//Sort could be done at insert
//but probabily this thread has time to do
std::sort(m_Timers.begin(),
m_Timers.end(),
[](const TimerInfo ti1, const TimerInfo ti2)

return ti1.m_TimePoint > ti2.m_TimePoint;
);
m_Sort = false;


auto now = clock_t::now();
auto expire = m_Timers.back().m_TimePoint;

if (expire > now) //can I take a nap?

auto napTime = expire - now;
m_Condition.wait_for(lock, napTime);

//check again
auto expire = m_Timers.back().m_TimePoint;
auto now = clock_t::now();

if (expire <= now)

TimerCall(m_Timers.back().m_User);
m_Timers.pop_back();


else

TimerCall(m_Timers.back().m_User);
m_Timers.pop_back();




template<class T, class TArg1>
friend void CreateTimer(TimerThread<T> timerThread, int ms, TArg1 arg1);

template<class T, class TArg1, class TArg2>
friend void CreateTimer(TimerThread<T> timerThread, int ms, TArg1 arg1, TArg2 arg2);

public:
TimerThread() : m_Stop(false), m_Sort(false)

m_Thread.reset(new std::thread(std::bind(TimerThread::TimerLoop, this)));


~TimerThread()

m_Stop = true;
m_Condition.notify_all();
m_Thread->join();

;

template<class T, class TArg1>
void CreateTimer(TimerThread<T> timerThread, int ms, TArg1 arg1)


std::unique_lock<std::mutex> lock(timerThread.m_Mutex);
timerThread.m_Timers.emplace_back(TimerThread<T>::TimerInfo(TimerThread<T>::clock_t::now() + std::chrono::milliseconds(ms),
std::forward<TArg1>(arg1)));
timerThread.m_Sort = true;

// wake up
timerThread.m_Condition.notify_one();


template<class T, class TArg1, class TArg2>
void CreateTimer(TimerThread<T> timerThread, int ms, TArg1 arg1, TArg2 arg2)


std::unique_lock<std::mutex> lock(timerThread.m_Mutex);
timerThread.m_Timers.emplace_back(TimerThread<T>::TimerInfo(TimerThread<T>::clock_t::now() + std::chrono::milliseconds(ms),
std::forward<TArg1>(arg1),
std::forward<TArg2>(arg2)));
timerThread.m_Sort = true;

// wake up
timerThread.m_Condition.notify_one();


//sample
#include <iostream>
#include <string>

void TimerCall(int i)

std::cout << i << std::endl;


int main()

std::cout << "start" << std::endl;
TimerThread<int> timers;

CreateTimer(timers, 2000, 1);
CreateTimer(timers, 5000, 2);
CreateTimer(timers, 100, 3);

std::this_thread::sleep_for(std::chrono::seconds(5));
std::cout << "end" << std::endl;


3. 这个问题是非常上下文驱动的。 斩钉截铁: a)你是否希望有一个基于回调:你需要另一个线程来维护基于时间间隔优先级队列,该线程可以睡的基础上,至少在它的优先级队列。 b)你是否想要一个非阻塞的基于回调(单线程):通常这是必需的,因为你所监听套接字来服务客户单线程服务器。工作线程可能是为了服务每个请求。但你要运行非阻塞模式。然后你就可以有选择/ epoll的等..在插座听取和FD基于使用创建

以上是关于为啥std:chrono:steady的主要内容,如果未能解决你的问题,请参考以下文章

std::chrono 和 cout

如何使用 std::chrono 库设置特定时间?

无法解析带有 NDK r10e 的 std::chrono

C++ std::chrono::seconds()函数(持续时间(秒))

std::chrono::time_point 线程安全吗?

`std::chrono` 时钟与`boost::xtime` 的比较