scoped_lock

scoped_guard can be used to lock multiple mutexes simultaneously. Resource deadlock is avoided if more than one mutex is locked at the same time.

#include <vector>
#include <iostream>

#include <thread>
#include <mutex>
#include <memory>

struct Node final
{
   ::std::vector
      <
      unsigned
      >
      links;
   
   ::std::shared_ptr
      <
      ::std::mutex
      >
      mutex;
   
   Node(void)
      :
      mutex
         (
         ::std::make_shared
            <
            ::std::mutex
            >
            ()
         )
      { }
};

::std::vector
   <
   Node
   >
   nodes;

void link_two_nodes(unsigned i, unsigned j)
{
   //
   // Lock the mutexes in both nodes:
   //
   
   ::std::scoped_lock
      guard
         (
         *nodes[i].mutex
            ,
         *nodes[j].mutex
         )
         ;
   
   //
   // Modify the nodes:
   //
   
   nodes[i].links.push_back( j );
   nodes[j].links.push_back( i );
   
   return;
}

int main(int argc, char ** argv)
{
   ::std::vector
      <
      ::std::thread
      >
      threads;
   
   for(unsigned i = 0; i< 6; ++i)
   {
      nodes.emplace_back();
   }
   
   //
   // Start many threads, each of which inserts a different
   // link between nodes:
   //
   
   for(unsigned i = 0; i< nodes.size(); ++i)
   {
      for(unsigned j = (i + 1); j< nodes.size(); j+=2)
      {
         threads.push_back
            (
            ::std::thread(link_two_nodes, i, j)
            )
            ;
      }
   }
   
   for(auto & thread : threads)
   {
      thread.join();
   }
   
   for(unsigned i = 0; i< nodes.size(); ++i)
   {
      ::std::cout << "Node "
                  << i
                  << " is linked to: "
                     ;
      
      for(auto const & record : nodes[i].links)
      {
         ::std::cout << record << " ";
      }
      
      ::std::cout << ::std::endl;
   }
   
   return 0;
}
Possible output:
Node 0 is linked to: 1 5 3 Node 1 is linked to: 0 2 4 Node 2 is linked to: 1 3 5 Node 3 is linked to: 0 2 4 Node 4 is linked to: 1 3 5 Node 5 is linked to: 0 2 4