[AGENT++] MT-safety of get_printable() (was: SNMP++ advocacy)

maom_onet at poczta.onet.pl maom_onet at poczta.onet.pl
Sat Jan 15 20:29:13 CET 2005


Użytkownik Don Radick <dradick at mindspring.com> napisał:
>On Fri, 2005-01-14 at 10:42 +0100, maom_onet at poczta.onet.pl wrote:
>> Użytkownik Don Radick <dradick at mindspring.com> napisał:
>> >On Wed, 2005-01-12 at 23:35 +0100, maom_onet at poczta.onet.pl wrote:
>> >> A possible approach would be:
>> >> 
>> >> if (addr_changed)
>> >> {
>> >>    char buf[LARGE_ENOUGH];
>> >>    format_output(buf);
>> >>    strcpy(output_buffer, buf);
>> >>    addr_changed = false;
>> >> }
>> >> return output_buffer;
>> >> 
>> >> There is some overhead but this guarantees "strong MT-safety".
>You are correct.  I was continuing to think in MT terms. and your
>solution will simply require the other thread to wait on the buffer
>copy.

Another MT-safe approach uses a temporary copy of an object, e.g.:

bool SafeGetPrintable(const Oid& oid, std::string& s) {

    Oid oidCopy(oid);
    if (!oidCopy.valid())
        return false;
    const char* ptr = oidCopy.get_printable();
    if (!ptr)
        return false;
    s = ptr;
    return s == ptr;    // be paranoid
}

Looks a bit paranoid, especially error checks, but the idea is to use a copy. Theoretically looks like a big overhead, and this is true to some degree; however, get_printable() in Oid class allocates dynamically a buffer (OctetStr seems to do the same), so when we use a temporary object, this buffer is not created nor held for our object.

But in fact, I think a final solution would be to provide get_printable(buf, buflen) functions.

Marek



More information about the AGENTPP mailing list