Log() { n = 0; done = false; output.open(LOG_FILE); if (!output.is_open()) { output.copyfmt(cout); output.clear(cout.rdstate()); output.basic_ios<char>::rdbuf(cout.rdbuf()); } logStartTime = chrono::high_resolution_clock::now(); t = thread([&]() { Report report; while (!done) { for (int i = 0; i < NUMBER_OF_QUEUES; ++i) while (queue[i].pop(report)) // XXX change to if to avoid starvation? priority.push(report); double now = chrono::duration_cast<chrono::duration<double>>( chrono::high_resolution_clock::now() - logStartTime).count(); while ((!priority.empty()) && ((now - priority.top().time) > 0.5)) { output << priority.top().text << endl; priority.pop(); } usleep(WAIT_MICROSECONDS); } for (int i = 0; i < NUMBER_OF_QUEUES; ++i) while (queue[i].pop(report)) priority.push(report); while (!priority.empty()) { output << priority.top().text << endl; priority.pop(); } }); }