【问题标题】:How to make a dictionary of synchronization objects?如何制作同步对象字典?
【发布时间】:2015-11-05 10:08:57
【问题描述】:

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

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

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

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

感谢您的回答!

【问题讨论】:

    标签: c++ mfc visual-c++-6


    【解决方案1】:

    使用指向关键部分的指针映射:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 2015-07-23
      • 1970-01-01
      • 2016-11-13
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多