Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling std::lock () with std::vector <mutex*>

I would like to replace the following code with std::lock():

for (mutex* m : mutexes) {
   m->lock();
}

Is there anyway I could invoke std::lock () on those mutexes given a std::vector<mutex*>?

like image 473
Jeremy Avatar asked Dec 25 '22 15:12

Jeremy


1 Answers

Unfortunately the standard library doesn't provide an overload for std::lock that takes a pair of iterators pointing to lockable objects. To use std::lock you must know the number of lockable objects at compile time, and pass them as arguments to the function. However, Boost does provide an overload that takes iterators, and it'll work with std::mutex.

The other piece of scaffolding you'll need is boost::indirect_iterator; this will apply an extra dereference when you dereference the iterator (needed because you have std::vector<std::mutex*> and not std::vector<std::mutex>. The latter would not be very useful anyway since std::mutex cannot be copied or moved.)

#include <boost/thread/locks.hpp>
#include <boost/iterator/indirect_iterator.hpp>

#include <vector>
#include <mutex>

int main()
{
    using mutex_list = std::vector<std::mutex*>;
    mutex_list mutexes;

    boost::indirect_iterator<mutex_list::iterator> first(mutexes.begin()), 
                                                   last(mutexes.end());
    boost::lock(first, last);
}

Live demo

like image 155
Praetorian Avatar answered Jan 09 '23 03:01

Praetorian