Sobes.tech
Junior — Senior

Analysis of lock safety in a function with two mutexes

livecode

Task condition

Conduct a review of the calculation function, which involves two std::mutex objects.

  1. Assess how correctly the synchronization is implemented.
  2. Identify the risk of deadlock.
  3. Propose a reliable method for acquiring multiple mutexes simultaneously.
void calculation(std::mutex& mxA, std::mutex& mxB, const std::vector<int>& input) {
    std::lock_guard<std::mutex> lock1(mxA);
    std::lock_guard<std::mutex> lock2(mxB);

    // data processing
}