This is part of a client-server setup in which the clients only have knowledge of each-other using their server-assigned indices. |
Here's what you don't seem to be understanding: You don't need an index for this.
Ask yourself the following questions:
1) Does the index itself have any significance other than being a unique identifier for each entry in the container?
2) Do you need to perform mathematical calculations on the index in order to retrieve other elements in the container (ie:
container[index - 10]
to get the element 10 places down)?
If the answer to the above questions is "no", then
there is no reason to use an index. An iterator will work just as well for your purposes, and can be used just fine with a list.
KISS. You don't need to design a whole new container. Just use std::list.
I was under the impression that when adding to a list, all of the indices were shifted to make room for the new addition |
Lists don't really have indexes.
But yes.. when you pop_front in a list, the order in which everything is stored changes...
but that doesn't matter because the elements themselves have not moved in memory.
What lists do have is iterators that remain valid even after you add/remove elements.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
std::list<int> mylist;
mylist.push_back(5);
std::list<int>::iterator i = mylist.begin();
std::cout << *i; // prints '5'
// add/remove a bunch of crap
mylist.push_back(0);
mylist.push_back(0);
mylist.push_front(0);
mylist.push_front(0);
mylist.push_back(0);
mylist.push_back(0);
mylist.pop_front();
mylist.pop_back();
// try again:
std::cout << *i; // still prints '5'. i hasn't changed.
// it doesn't matter how many times you add/remove from the list... as long as you don't remove
// the element i is referring to. i will never change/move.
|
Do most users really prefer to define their own mutexes? |
When appropriate, yes. It's an encapsulation thing. It's about organizing/assigning responsibilities between different areas of the program.
The mutex should be owned by whoever is responsible for locking it. If the container owns the mutex, then the container should be the
only one that is locking it. The user should know nothing about it.
However since the container cannot do that job adequately, and the user still has to be responsible for the mutex themselves, it doesn't really make any sense for the container to have the mutex.