Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a map of volatile elements in cpp

Tags:

c++

map

volatile

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?

like image 677
mousomer Avatar asked Oct 10 '12 14:10

mousomer


1 Answers

C/C++'s volatile is not like volatile in some managed languages and has nothing to do with mutexes (see http://www.drdobbs.com/parallel/volatile-vs-volatile/212701484). Depending on what you want to achieve consider using either std::mutex (or boost::mutex if you have a pre-C++11 standard library) or std::atomic<YourPreferredIntergalType>

like image 88
Arne Mertz Avatar answered Nov 15 '22 07:11

Arne Mertz