#include #include #include #include #include using namespace std; atomic a(0), b(0); mutex m; void suma() { while (true) { m.lock(); ++a; ++b; m.unlock(); } } void resta() { while (true) { m.lock(); --a; --b; m.unlock(); } } void muestra() { while (true) { m.lock(); cout << "a = " << a << "\t b = " << b << endl; m.unlock(); this_thread::sleep_for(250ms); } } int main() { thread t[3] = {thread(suma), thread(resta), thread(muestra)}; for (auto &i: t) i.join(); }