Code Monkey home page Code Monkey logo

learn_chrono's People

Contributors

tnie avatar

Stargazers

 avatar

Watchers

 avatar  avatar

learn_chrono's Issues

linux 和 windwos 时钟精度的差别

在 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;

}

在上述试验场景中猜测:

只有高精度的向低精度的转换时才必须显式转换(毫秒转秒,分钟转小时);低精度赋值给高精度时无需显式转换。

steady_clock 不是从 1970 年算起?

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.