Pergunta de entrevista da empresa Momentis Systems

asking for c++ design system, I asked to write c++ system that run 3 threads parallel and every thread print it's on charcter only, the order must be kept. example : the print will be : abcabcabc .... , another question : about memory allocation, mutex, semaphore.

Resposta da entrevista

Sigiloso

14 de fev. de 2026

create worker thread, create class has state, when reach the state it print the charcter. use one common mutex for the class . save state and number of threads , it is the counter. counter + = 1; counter = counter % num_threads; this is to make sure it turn will not pass the number of threads. before destructions wait until all threads will finished the job. the worker function example : void worker(std::stop_token st, int id, char ch) { std::stop_callback callback(st, [this] { _cv.notify_all(); }); while (true) { std::unique_lock lock(_mtx); _cv.wait(lock, [&] { return _turn == id || st.stop_requested(); }); if (st.stop_requested()) { std::cout << "worker id " << id << " stopped\n"; return; } std::cout << "print " << ch << std::endl; _turn = (_turn + 1) % _threadCount; _cv.notify_all(); } }