How can I use something like std::vector<std::mutex>?
Solution 1
vector
requires that the values are movable, in order to maintain a contiguous array of values as it grows. You could create a vector containing mutexes, but you couldn't do anything that might need to resize it.
Other containers don't have that requirement; either deque
or [forward_]list
should work, as long as you construct the mutexes in place either during construction, or by using emplace()
or resize()
. Functions such as insert()
and push_back()
will not work.
Alternatively, you could add an extra level of indirection and store unique_ptr
; but your comment in another answer indicates that you believe the extra cost of dynamic allocation to be unacceptable.
Solution 2
If you want to create a certain length:
std::vector<std::mutex> mutexes;
...
size_t count = 4;
std::vector<std::mutex> list(count);
mutexes.swap(list);
Solution 3
You could use std::unique_ptr<std::mutex>
instead of std::mutex
. unique_ptr
s are movable.
Solution 4
I suggest using a fixed mutex pool. Keep a fixed array of std::mutex
and select which one to lock based on the address of the object like you might do with a hash table.
std::array<std::mutex, 32> mutexes;
std::mutex &m = mutexes[hashof(objectPtr) % mutexes.size()];
m.lock();
The hashof
function could be something simple that shifts the pointer value over a few bits. This way you only have to initialize the mutexes once and you avoid the copy of resizing the vector.
Solution 5
If efficiency is such a problem, I assume that you have only very small data structures which are changed very often. It is then probably better to use Atomic Compare And Swap (and other atomic operations) instead of using mutexes, specifically std::atomic_compare_exchange_strong
Walter
programming numerical algorithms for astrophysical applications.
Updated on October 23, 2021Comments
-
Walter over 2 years
I have a large, but potentially varying, number of objects which are concurrently written into. I want to protect that access with mutexes. To that end, I thought I use a
std::vector<std::mutex>
, but this doesn't work, sincestd::mutex
has no copy or move constructor, whilestd::vector::resize()
requires that.What is the recommended solution to this conundrum?
edit: Do all C++ random-access containers require copy or move constructors for re-sizing? Would std::deque help?
edit again
First, thanks for all your thoughts. I'm not interested in solutions that avoid mutices and/or move them into the objects (I refrain from giving details/reasons). So given the problem that I want a adjustable number of mutices (where the adjustment is guaranteed to occur when no mutex is locked), then there appear to be several solutions.
1 I could use a fixed number of mutices and use a hash-function to map from objects to mutices (as in Captain Oblivous's answer). This will result in collisions, but the number of collisions should be small if the number of mutices is much larger than the number of threads, but still smaller than the number of objects.
2 I could define a wrapper class (as in ComicSansMS's answer), e.g.
struct mutex_wrapper : std::mutex { mutex_wrapper() = default; mutex_wrapper(mutex_wrapper const&) noexcept : std::mutex() {} bool operator==(mutex_wrapper const&other) noexcept { return this==&other; } };
and use a
std::vector<mutex_wrapper>
.3 I could use
std::unique_ptr<std::mutex>
to manage individual mutexes (as in Matthias's answer). The problem with this approach is that each mutex is individually allocated and de-allocated on the heap. Therefore, I prefer4
std::unique_ptr<std::mutex[]> mutices( new std::mutex[n_mutex] );
when a certain number
n_mutex
of mutices is allocated initially. Should this number later be found insufficient, I simplyif(need_mutex > n_mutex) { mutices.reset( new std::mutex[need_mutex] ); n_mutex = need_mutex; }
So which of these (1,2,4) should I use?
-
Walter about 11 yearsand create each mutex individually on the heap? are you kidding?
-
Mike Seymour about 11 years@Walter: It's a perfectly sensible suggestion, if the extra memory footprint and the cost of the extra level of indirection are acceptable.
-
Walter about 11 years@MikeSeymour and the cost of allocating & de-allocating many individual mutexes. Well this is a horrible workaround. Really, I don't want to copy mutexes at all. All I want are default constructed mutexes in a number that I can alter. Unfortunately, vector::resize() cannot do that ...
-
Walter about 11 yearsWhat about
std::unique_ptr<std::vector<std::mutex>>
? On construction, the vector will use the default constructor of its elements, so not problem. -
Walter about 11 yearsI also thought about
std::deque
. Why would I need to useemplace()
? Wouldn't the constructor (of the deque) simply use the default constructor of its elements? -
Mike Seymour about 11 years@Walter: Indeed, using the constructor is fine. I meant that you'd have to use
emplace()
rather thanpush_back()
to add individual mutexes, but I didn't say that clearly enough. -
GManNickG about 11 years@Walter: Why don't you create a class that keeps the mutex associated with its object? Then write custom copy- and move-constructors, and assignment-operators, that allow your class to be used in containers just fine. Keeping bare mutex's around seems a bit silly.
-
Mike Seymour about 11 years@Walter: You can't use a vector if you want to resize it; wrapping it in a
unique_ptr
won't make any difference. If you have a fixed number, then a suitably constructed vector should work; but you say "potentially varying", which rules out usingvector
. -
Walter about 11 yearsI didn't mean to
resize()
but instead to delete and re-construct the wholevector
of mutexes. This should be as fast asresize()
would have been. In this case, I think using aunique_ptr
to hold thevector
is the cleanest way, though perhaps one could destruct and re-construct in place (?). -
Walter about 11 yearsinteresting idea. It's not guaranteed that there are no two objectPtr resulting in the same mutex, causing more contention. I presume that this is okay so long as the number of mutexes is larger (by how much) than the number of threads.
-
Mike Seymour about 11 years@Walter: OK, I see what you mean now. That should work, but make sure you never do that while any mutex is locked.
-
Captain Obvlious about 11 yearsCorrect, you would experience collisions. If those collisions don't affect you this may be a more efficient alternative.
-
Walter about 11 yearsI already used many fewer mutexes than actual ocbjects and groups the objects into chunks, because whenever a thread has to write into one objects, it is likely to also write into a nearby one.
-
Walter about 11 yearswhat about
std::unique_ptr<std::mutex[]>
, which is initialised to an appropriate size (new std::mutex[N]
) and if that proves to be too small at later times, simply re-allocated? To me this looks like the best solution. -
Matthias Benkard about 11 years@Walter In that case, you will need to recreate all mutexes on resize. You can only safely do so when no existing mutex is currently held. Can you ensure that?
-
Aaron McDaid over 8 yearsThis answer confirms that
vector
doe not require move-constructible items. It depends on what you do with thevector
. You can grow avector<mutex>
in one way (only one way, yourswap
trick), but you can easily shrink such a vector and you can (as in your answer) ensure it has the right size to begin with. -
Supamee about 7 yearsI fail to see why this answer isn't the preferred? How is this functionally different then .resize() other than the larger overhead. what do you mean by it can only grow in one way? (sorry if this isn't the correct format, I'm new to the forum)
-
Supamee about 7 yearsAfter messing around with it I now see why; if you use .swap() to grow the vector, all the elements are new.
-
lvella over 3 yearsI wonder if this is an implementation quirk, or this should work on every conformant standard C++ library.