/* * Compile with g++ -pthread -std=c++11 -O0 -g3 -o bug bug.cc */ #include #include #include #include #include #include int depth; void bp1 (int t, int level, int n) { auto r = std::rand () % (t + level + n); std::this_thread::sleep_for (std::chrono::milliseconds (r)); } void bp2 (int t, int level, int n) { auto r = std::rand () % (t + level + n); std::this_thread::sleep_for (std::chrono::milliseconds (r)); } void sleeper (int t, int level, int n) { int loop = 0; if (++level < depth) sleeper (t, level, n); else while (true) { if (++loop % 2) bp1 (t, level, n); else bp2 (t, level, n); } } int main (int argc, char *argv[]) { auto max = argc > 1 ? std::atoi (argv[1]) : 2; depth = argc > 2 ? std::atoi (argv[2]) : 8; std::vector T; for (auto i = 0; i < max; i++) { auto t = new std::thread (sleeper, i, 0, 1000); pthread_setname_np (t->native_handle (), (std::string ("worker ") + std::to_string (i)).c_str ()); T.push_back (t); } for (auto &t: T) t->join (); return 0; }