Reputation: 5004
Hi I want to maintain a list of semaphores created and their allowed count (no of thread instances allowed to access that semaphore) value. I tried with a map in stl like
map<sem_t, int> sem_map;
so that the key(sem_t) has the count as value in the map.
but c++ is not allowing me to use sem_t as a key as it is (sem_t) a union type. So, how can i maintain a semaphore list? My goal is whenever i want to lock or unlock the semaphore i first get the id of the semaphore from my maintained list and do some pre-processing with it.
Upvotes: 0
Views: 763
Reputation: 21900
You could use the sem_t's address as the key:
map<sem_t*, int> sem_map;
Then, given a sem_t x, you could access the int value in the map using:
sem_map.find(&x);
Note that this solution works only if you are not copying semaphores around.
EDIT: the reason why you couldn't use sem_t as keys in your map is that std::map requires that the key type has operator< defined(in order to do internal sorting, std::map(s) are tree structures). However, a union does not have a operator< defined, so the compiler won't let you use it. On the other hand, pointers can be compared between them, so the compiler will actually let you use them as keys in your std::map.
Upvotes: 2