tnie / learn_chrono Goto Github PK
View Code? Open in Web Editor NEW学习 C++11 中的日期和时间库等等
学习 C++11 中的日期和时间库等等
类似的,boost::thread
和 std::thread
等等
boost::array
和 std::array
在 widows 下发现 system_clock
的精度介于纳秒和微秒之间,怪纠结的。
#define _XTIME_NSECS_PER_TICK 100
#define _XTIME_TICKS_PER_TIME_T (long long)10000000
struct system_clock
{ // wraps GetSystemTimePreciseAsFileTime/GetSystemTimeAsFileTime
typedef long long rep;
typedef ratio_multiply<ratio<_XTIME_NSECS_PER_TICK, 1>, nano> period;
我觉得 linux 不会做这种蛋疼的事情(尚未验证),所以 linux 和 windows 的时钟精度可能就会有差异了。
auto twoseconds = chrono::duration<int>(2);
//auto twoseconds = chrono::seconds(2);
chrono::milliseconds alias = twoseconds; // 这都行-拷贝构造可以
{
alias = twoseconds; // 低精度赋值给高精度可以
//twoseconds = alias; // 高精度赋值给低精度必须显式转换
}
{
// 自定义时间间隔:半秒
typedef chrono::duration<double, ratio<1, 2>> halfseconds;
auto var = halfseconds(2.5); // 1.25 秒
cout << "1.25s has " << var.count() << " halfseconds" << endl;
//alias = var; // 需要显示转换
alias = chrono::duration_cast<chrono::milliseconds> (var);
cout << "1.25s has " << alias.count() << " milliseconds" << endl;
}
在上述试验场景中猜测:
只有高精度的向低精度的转换时才必须显式转换(毫秒转秒,分钟转小时);低精度赋值给高精度时无需显式转换。
auto t2 = chrono::steady_clock::now();
cout << setw(20) << "now(nano) is: " << t2.time_since_epoch().count() << endl; // TODO steady_clock 不是从 1970 年算起?
auto t3 = chrono::system_clock::now(); // 查看底层实现,发现 system_clock 比 steady_clock 精度低100 倍
cout << setw(20) << "now(nano*100) is: " << t3.time_since_epoch().count() << endl;
上述输出并非前者比后者大 100 倍
now(nano) is: 110120167767890
now(nano*100) is: 15233445249489464
template<typename T> void print_queue(T& q) {
while (!q.empty()) {
std::cout << q.top() << " ";
q.pop();
}
std::cout << '\n';
}
template<typename a, typename b> void print_queue(priority_queue<int, a, b> & q) {
while (!q.empty()) {
std::cout << q.top() << "[" << (q.top() ^ 1) << "] ";
q.pop();
}
std::cout << '\n';
}
int main()
{
// 用 lambda 比较元素。
auto cmp = [](int left, int right) { return (left ^ 1) < (right ^ 1); };
std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);
for (int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2})
q3.push(n);
print_queue(q3);
}
为什么就会优先找第二个了呢?上述用法其实并不涉及特例化,所以就是模板函数匹配优先级喽
template<class T>
class MutexQueue : public std::queue<T>
{
public:
void push(const value_type& x)
{
std::lock_guard<std::mutex> _lock(_mutex);
if (find(c.begin(), c.end(), x) == c.end())
{
__super::push(x);
}
}
void pop()
{
std::lock_guard<std::mutex> _lock(_mutex);
__super::pop();
}
size_type size()
{
std::lock_guard<std::mutex> _lock(_mutex);
return __super::size();
}
const_reference front()
{
std::lock_guard<std::mutex> _lock(_mutex);
const_reference ret = __super::front();
return ret;
}
private:
std::mutex _mutex;
};
cout << "sleep 2s ..." << endl;
//this_thread::sleep_for(twoseconds);
this_thread::sleep_for(2s); //TODO 这都行啊,这是如何实现的?
cout << "END" << endl;
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.