[AGENT++] MT-safety of get_printable() (was: SNMP++ advocacy)
Don Radick
dradick at mindspring.com
Thu Jan 13 23:19:08 CET 2005
On Wed, 2005-01-12 at 23:35 +0100, maom_onet at poczta.onet.pl wrote:
> > Well, I think that there are two options:
> > - call get_printable() just after an object has been created, to
> > pre-format its buffer and seal the state
> > - use locks to synchronize access
>
> Unfortunately, I did not notice that both method just won't work.
> Only address classes behave in the first way, and locks will not
> help if SNMP++ calls get_printable() internally
> (which does happen, although I have not analyzed it carefully).
>
> 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".
>
Not unless strcpy is an atomic operation.
During the cycles for the strcpy, the output_buf[] is invalid.
You'll need to use a critical section, a semaphore, or a MT-safe strcpy.
-Don
More information about the AGENTPP
mailing list