Re: Thread-safe reference counts.
On 31 =CD=C1=D2, 02:48, David Schwartz <dav...@webmaster.com> wrote:
This seems an awful lot of work to create a problem followed by an
awful lot of work to solve it. Just never allow a pointer to exist
without a reference. It seems illogical to allow this anyway, since
the pointer can't be guaranteed to point anywhere useful anyway.
If you get a pointer from somewhere, that somewhere must have the
pointer. Thus it must have a reference -- otherwise the pointer might
be invalid. Since the pointer is accompanied by a reference, there is
no risk that the pointer can become invalid until you get your
reference. All you have to do is sequence the operations that require
or involve the reference that accompanies that copy of the pointer.
Consider following example:
class application_settings;
application_settings* g_settings;
// replaces g_settings with new_settings,
// and releases previous value of g_settings
void update_settings(application_settings* new_settings);
// acquires and returns object stored in g_settings
application_settings* acquire_settings();
// releases settings object
void release_settings(application_settings* settings);
void reader_thread()
{
for(;;)
{
application_settings* settings = acquire_settings();
// use settings
release_settings(settings);
}
}
In this example acquire_settings() must act on pointer to object for
which it doesn't have reference yet.
How this pattern must be implemented in the right way (pointer ==
reference)?
Dmitriy V'jukov