I just solved this exercise from https://www.stroustrup.com/4thExercises.pdf Page 3
/*
Write a program with two treads: one that writes hello every second and one that writes world! every second
*/
Code:
#include <iostream>
#include <thread>
#include <chrono>
void print(const char* msg)
{
while (true)
{
std::cout << msg;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
};
int main()
{
std::thread t1(print, "Hello ");
std::thread t2(print, "world!");
t1.join();
t2.join();
}
Does anyone see a better solution ?