1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include <iostream> #include <thread>
static bool is_finished = false;
void DoWork() { while (!is_finished) { using namespace std::literals::chrono_literals;
std::cout << "working...\n"; std::this_thread::sleep_for(1s); } }
int main() { std::thread worker(DoWork);
std::cin.get(); is_finished = true;
worker.join(); std::cout << "Finished!" << std::endl;
std::cin.get(); return 0; }
|