C++基礎-等待固定時間cv.wait_until(lk, end)(等待固定時間) chrono::high_resolution_clock::now()(獲得當前時間)


使用cv.wait_unitil(lk, end)等待固定的時間 

int run()
{
    auto start = chrono::high_resolution_clock::now(); //當前時間
    auto end = start + chrono::milliseconds(5000); //結束時間
    unique_lock<mutex> lk(m);
    while(!done)
    {
        if(cv.wait_until(lk, end) == cv_status::timeout){
            done = true;
            break;
        }
    }
    system("pause");



}

int main1()
{
    thread t1(run);

    cin.get();
}

計算程序執行的時間

int main()
{
    time_t t1, t2;
    auto start = chrono::high_resolution_clock::now(); //當前時間
    t1 = time(&t1);
    double db = 0;
    for(int i = 0; i < 1000000000; i++){
        db += i;
    }
    t2 = time(&t2);
    auto end = chrono::high_resolution_clock::now();
    cout << (end - start).count() << endl;
    cout << difftime(t2, t1) << endl;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM