如何制作同步对象的字典?

时间:2015-11-05 10:08:58

标签: c++ mfc vc6

我需要的是std::map<std::string, CCriticalSection>,但CCriticalSection是不可复制的。而不是CCriticalSection我认为我可以使用CRICITAL_SECTION,但它也是not possible to copy or move objects of this type。因为这是一个非常古老的项目,我只能使用MFC和VC6。我想以下面的方式访问同步对象(以下代码不起作用,只是一个想法,我想如何使用dicionary):

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

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

所以我的问题是,如何使用MFC和VC6创建同步对象的字典?

感谢您的回答!

1 个答案:

答案 0 :(得分:4)

使用指向关键部分的指针图:

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;