Mutexes ~~lol~~

Just a quick question on mutexes (looked up the correct plural form, this is as close as it comes)

When I use a mutex, how would I go about checking if it's locked? I don't want to lock it when I check, I just want to see the state.

(reason being): I have a vector of pairs of sockets or partners of connected strangers and I need to check the vector every second or half second and I will have to change the sockets around only occasionally so I really don't need to lock the whole vector to check just a single member. (I was thinking of having a vector of mutexes but that just seemed silly)

On a side note, what stl container would be best for a pair of ints, in which the key to eachother is the partner's int?
Couldn't really tell as I don't know what mutexes you are talking about (Windows ones?). Besides, I think that locking the whole vector seems to be reasonable (although I may be wrong as I don't really know exactly what your program is doing).

As for the container, I believe you need fast searching by either the first or the second element of a pair. No stl container supports that. However, there is a question: how many pairs you will typically have? If it is not a significant number then maybe even a slow search algorithm (iterating a list or vector) will be sufficient.
ultifinitus wrote:
how would I go about checking if it's locked?
not at all. It makes no sense to check whether it's locked. You want to protect a variable and it doesn't matter it you just use a part of it.

ultifinitus wrote:
On a side note, what stl container would be best for a pair of ints, in which the key to eachother is the partner's int?
map. But it's sorted if speed doesn't matter so what.
index indices
vertex vertices
mutex mutices
pokedex pokedices
s/(\w*[dt])ex/\1ices/
Last edited on
Regarding the container you could try Boost intrusive set for example. I am not very familiar with it, but I sure can't think of appropriate STL solution.

http://www.boost.org/doc/libs/1_45_0/doc/html/intrusive/set_multiset.html

Regards
@abramus & coder777
Well the program is a server serving pairs of anonymous clients, so it is time sensitive, initially...
double edit: sorry didn't mention what the pairs of ints are, they are simple pairs of SOCKET (or uint)

@rocketboy9000
http://stackoverflow.com/questions/866837/plural-form-of-word-mutex
triple edit: lol at pokedex

@simeonz
I just figured the standard library would be a good way to go, i'll look into boost intrusive.


---

When you say a map is sorted, it's only sorted when an object is added or removed correct?
And I looked into the documentation it mentioned a multimap, how efficient would that solution be compared to a simple vector solution (a vector of structs which hold the key to the pair's vector member)?

edit: i'm going to do a little testing on both solutions and i'll let you know what I find, I would still like to know what your preferred method would be
Last edited on
Yeah that was a joke, I know English isn't the logical language we all would like it to be.
Haha Ya got that =D
Topic archived. No new replies allowed.