3

What I need is something like std::map<std::string, CCriticalSection> but CCriticalSection is non-copyable. Instead of CCriticalSection I thought I could use CRICITAL_SECTION but it's also not possible to copy or move objects of this type. Because it's a very old project, I'm restricted to use MFC and VC6. I would like to access the synchronization objects in following manner (the following code does not work and is just an idea how I would like to use the dicionary):

// global variable
std::map<std::string, CCriticalSection> csec;

unsigned int somefunc(std::string ip)
{
    CSingleLock lock(&csec[ip], TRUE);
    // do something
}

So my question is, how to make a dictionary of synchronization objects using MFC and VC6?

Thank you for your answers!

Community
  • 1
  • 1
Christian Ammer
  • 7,464
  • 6
  • 51
  • 108

1 Answers1

4

Use a map of pointers to critical sections:

std::map<std::string, CCriticalSection *> csec;

// add
csec["key1"] = new CCriticalSection();

// access
CSingleLock lock(csec[ip], TRUE);

// don't forget to delete after use
for (std::map<std::string, CCriticalSection *>::iterator i = csec.begin();
     i != csec.end(); ++i)
    delete i->second;
Ilya Popov
  • 3,765
  • 1
  • 17
  • 30