c++ - std::map 是否锁定其节点以防止其他进程删除它们?

标签 c++ locking stdmap plc

我遇到了一个严重的问题。 我有两个不同的任务(比如一个进程)在我的 PLC X20 窗体 B&R 上运行,访问一个单例对象“VarList”。它的目的是允许通过指针进行进程间通信。

任务 A 正在使用静态 getInstance() 方法创建包含 std::map 的对象。如果该对象被分配给全局 PLC 变量以允许从每个任务访问该特定对象,则指针在那里。该任务还检查每个任务周期是否将变量插入到该映射中。在这种情况下,它会尝试再次删除该插入的节点,仅用于测试目的。

任务 B 通过 getInstance 方法获取对象的指针,并将 bool* 插入到映射中。

之后任务 A 尝试再次删除它,但它因访问冲突而崩溃。我可以访问节点及其值。我可以改变那个值(value)观。但是删除在不同任务中创建的节点会导致崩溃,我想知道为什么!

PLC 是单线程的,所以应该没有对内存的并发访问。

这是一个锁定问题吗?或者这是一个逻辑问题? STL 问题?空指针问题?或者又是 plc 供应商的 std 库的具体实现问题?感谢任何与访问冲突相关的帮助!

这里是提到的对象的代码片段,我删除了所有与问题无关的东西,只是为了得到一个提示。抱歉,它很可能不可编译:

编译器:gcc 4.1.2

任务A:

#include <VarListe.hpp>
VarListe::Ptr VLInstanz;

void _INIT VLErzeugerInit(void)
{
    VLInstanz = VarListe::getInstance("VLErzeuger");
}

void _CYCLIC VLErzeugerCyclic(void)
{
    VLInstanz->checkNewVars(); // Access Violation here
}

任务 B:

#include <VarListe.hpp>
VarListe::Ptr vals;

bool setPtr = true;


void _INIT VarListeTestInit(void)
{
}

// btn_VarTest is a Boolean plc Variable for a button on the Visu
void _CYCLIC VarListeTestCyclic(void)
{
    try
    {
        if(btn_VarTest &&setPtr) 
        {
            vals = VarListe::getInstance("VarListe1");
            vals->setVar("btn_VarTest",&btn_VarTest);   
            //vals->checkNewVars(); // Works perfect if used here. Thats not the point
            setPtr = false;
        }
    catch (...)
    {
    }
}

变量列表.hpp

#include <map>
#include <deque>
#include <string>
#include <boost/shared_ptr.hpp>

using namespace std;

class VarListe
{
    public: 

    typedef bool*           BoolPtr;

    typedef boost::shared_ptr<VarListe> Ptr;

    static Ptr getInstance(string owner);
    static Ptr _alwaysUseGetInstance;
    static char owners[200];

    void checkNewVars();

    private:
    typedef map<string, BoolPtr >           BoolPtrMap;

    typedef deque<BoolPtrMap::iterator>     BoolVarQueue;
    BoolVarQueue                            _boolVarQueue;

    BoolPtrMap      _boolListe;

    public:
    void setVar(string key, bool* value);
};

变量列表.cpp

#include <VarListe.hpp>

VarListe::Ptr VarListe::_alwaysUseGetInstance; // Singleton static Variable; used olny by the object creator
char    VarListe::owners[200]; // just to test which task creats the object

#include <../../Temp/Includes/globalvar.h> // For the global PLC variable 'GlobalVarListe' 

// This static Method runs perfectly, no need to check here
VarListe::Ptr VarListe::getInstance(string owner)
{

    if(GlobalVarListe == 1337)
    {
        strcpy(VarListe::owners, "");
        owner += "(Builder)";
        if (!VarListe::_alwaysUseGetInstance)
            VarListe::_alwaysUseGetInstance = VarListe::Ptr(new VarListe);
        GlobalVarListe = (UDINT) &VarListe::_alwaysUseGetInstance;
        VarListe::_alwaysUseGetInstance->setVar("VarListOwners",VarListe::owners);
    }
    VarListe::Ptr tempVL = *( (VarListe::Ptr*) GlobalVarListe);
    VarListe::CharPtrPair locOwners;
    if(tempVL->assertVar("VarListOwners",locOwners))
    {
        string temp = string(locOwners.first);
        temp = temp + owner + " + ";
        strcpy(locOwners.first, temp.c_str());
    }

    return tempVL;
}

// This Method is used in Task B
void VarListe::setVar(string key, bool* value)
{
    pair<BoolPtrMap::iterator, bool > eingetragen;
    eingetragen = _boolListe.insert(pair<string, BoolPtr>(key, value ));

    if(eingetragen.second == false)
    {
    } 
    else
        _boolVarQueue.push_back(eingetragen.first);
}

// This method is in Task A
void VarListe::checkNewVars()
{
    if(!_boolVarQueue.empty())
    {
        string key = _boolVarQueue.front()->first;  //OK, 
        BoolPtr bp = _boolVarQueue.front()->second; //OK
        _boolVarQueue.front()->second = 0;          //OK
        _boolListe[key] = bp;                       //OK
        BoolPtrMap::iterator fund = _boolListe.find(key); // OK
        if (fund != this->_boolListe.end())         // OK
        {       
            _boolListe.erase(key);                      //Access Violation: Code 9101 only if used by Task A 
            _boolListe.erase(_boolVarQueue.front());    //Access Violation: Code 9101 
            _boolListe.erase(fund);                     //Access Violation: Code 9101
        }
        _boolVarQueue.pop_front();                  //OK
        _boolListe[key] = bp;                       //OK    
    }
}

谢谢!

最佳答案

将 STL 映射放置在具有独立地址空间的进程(或任务)之间的任何类型的共享内存中,如果不进行更改,则无法可靠地完成。

问题在于,如果进程 A 将数据插入到映射中,它将从其自己的地址空间分配任何新内存(并且映射会在内部为新条目分配内存),进程 B 无法访问这些内存。因此,如果进程 B 然后尝试访问新插入的数据,它很可能会出错(例如访问冲突)。

幸运的是,STL 容器允许覆盖分配器以将其替换为您自己的。如果您自己的分配器然后确保它从共享内存而不是进程自己的地址空间分配内存,那么事情应该会更好。

关于c++ - std::map 是否锁定其节点以防止其他进程删除它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16300685/

相关文章:

c++ - 在 Visual Studios 2013 中是否有某种资源监视器?

c++ - 为什么 "long int"与 "int"大小相同?这个修改器到底有没有用?

c++ - 使用 C++ lock_guard 时如何收紧范围?

c++ - 如何在两个对象之间干净地传递 std::map ?

c++ - 在锁下清除 std::map 与移动到临时对象

c++ - 在 std::sort() 中完成的比较次数

c++ - Ceres 求解器 : using smooth approximations for non-linear least squares

java - hazelcast IMap 只能用于锁定目的吗?

perl - 我可以让 fcntl 和 Perl 警报合作吗?

c++ - 带有提示的 std::map::insert_or_assign 的复杂性