Boost logo

Boost :

From: E. Gladyshev (egladysh_at_[hidden])
Date: 2002-03-25 16:37:56


I've decided to start another thread,
since my original "plain pointers and shared_ptr"
caused some confusion.

To understand what I was talking about,
here is a puzzle :).

Objective:
   implement a smart pointer
   shared_pointer<int> and function foo( int* px )
   that does something on *px. foo() can
   use shared_pointer<int> but only internally.

Requirements:
1. There must be only one version of foo() without
   any conditional compiling.

2. The foo() client and foo() can be running
   in different threads.

3. It should be obvious how to implement
shared_pointer<int>
   in multi-thread environment.

4. foo() has to work correctly if the client uses
   shared_pointer<int> to share the 'px' object.
   In other words, if shared_pointer<int> goes out
   of scope on the client side while foo() is still
   using the object pointed by 'px' (can happen in
multi-thread
   applications), the program should not crash.
   foo() has to call the object destructor when
   it exits and nobody else references the object.

5. foo() has to work consistently if the client
   is not using shared_pointer<int> ( a legacy
   C-code).

6. Following solutions are not accepted.
    - Solution 1. it is not possible.
    - Solution 2. change the foo() interface.
    - Solution 3. create multiple versions of foo().

Here are some foo() usages:

main()
{
   {
      shared_pointer<int> x( new int );
      foo( x.get() );
      //make sure that foo() is started
      ...
   } //forget about 'x' here

   //do something else
   ...

   //make sure that foo() is done.
   ...
   return 0;
}

main()
{
   int x;
   foo( &x );
   //wait when foo() is done.
}

main()
{
    int *px = new x;
    foo( px );

    //wait when foo() is done.
    delete px;
}

See a proposed solution in my first
"plain pointers and shared_ptr" message.

Anybody has a better idea?

__________________________________________________
Do You Yahoo!?
Yahoo! Movies - coverage of the 74th Academy Awards®
http://movies.yahoo.com/


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk