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