Search code examples
c++dictionaryvolatile

How to define a map of volatile elements in cpp


I want to generate a map of mutex elements, which probably need to be volatile. I am trying to define something of this sort:

typedef std::map<int key, volatile long mutex> tMutexMap;
tMutexMap myMutexMap;

and then use the simple map index, e.g.:

myMutexMap[10]=0;
myMutexMap[23]=1;

I'm using gcc 4.1.2. The gcc compiler doesn't like that code. I get an error

no matching function for call to 'std::pair....'"

What am I doing wrong?


Solution

  • I'm not sure that you can make volatile element in map. You can make volatile map but not value in map. Map is implemented as binary tree. And there is complex algorithm to get value from this tree. Volatile said to compiler that it shouldn't optimize access to this variable. You can use volatile when you have direct access to variable. But in map you don't have such direct access.