[AGENT++] scoped_lock usage to be exception save and simplify the MIB code

Claus Klein claus.klein at arcormail.de
Thu Jun 8 22:45:17 CEST 2017


Hallo,

I’m working for a long time with agentpp.
In the meantime the current c++ std is C++14, C++17 is coming soon!

What do you think about this new language features?

Threads, mutex, locks, atomic, timer, clock, … and more!

For example, with boost most of them is available with C++03 too:

// from http://www.boost.org/doc/libs/1_60_0/libs/smart_ptr/sp_techniques.html#as_lock <http://www.boost.org/doc/libs/1_60_0/libs/smart_ptr/sp_techniques.html#as_lock>
class scoped_lock
{
private:
    boost::shared_ptr<void> pv;

public:

    template<class Mutex> explicit scoped_lock(Mutex & m): pv((m.start_synch(), &m), mem_fn(&Mutex::end_synch)) {}
};


void netSnmpIETFWGEntry::update(Request* req)
{
    // This table needs to be updated.
    //--AgentGen BEGIN=netSnmpIETFWGEntry::update
    static unsigned long currentRequest = 0;
    static time_t lastUpdate = 0;
    time_t currentTime = time(NULL);    //FIXME use clock_gettime()

    if ((currentTime - lastUpdate) < 3) {
        return;
    }

    if (currentRequest == req->get_request_id()) {
        return;
    }
    currentRequest = req->get_request_id();
    lastUpdate = time(NULL);

    //====================================
    //ORIG: start_synch();
    scoped_lock(*this);
    //====================================

    if (netSnmpExampleSleeper::instance) {
        long l = 0;
        netSnmpExampleSleeper::instance->get_value(l);
        if (l > 0) sleep(l);
    }

    //====================================
    //ORIG: end_synch();
    //====================================

    //--AgentGen END
#ifdef MIB_TRACE
    std::cerr << BOOST_CURRENT_FUNCTION << std::endl;
#endif // MIB_TRACE
}

With Regards
Claus

 


More information about the AGENTPP mailing list