Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Class thread_specific_ptr

thread_specific_ptr();
explicit thread_specific_ptr(void (*cleanup_function)(T*));
~thread_specific_ptr();
T* get() const;
T* operator->() const;
T& operator*() const;
void reset(T* new_value=0);
T* release();
//  #include <boost/thread/tss.hpp>

namespace boost
{
  template <typename T>
  class thread_specific_ptr
  {
  public:
      thread_specific_ptr();
      explicit thread_specific_ptr(void (*cleanup_function)(T*));
      ~thread_specific_ptr();

      T* get() const;
      T* operator->() const;
      T& operator*() const;

      T* release();
      void reset(T* new_value=0);
  };
}

Requires:

delete this->get() is well-formed.

Effects:

Construct a thread_specific_ptr object for storing a pointer to an object of type T specific to each thread. The default delete-based cleanup function will be used to destroy any thread-local objects when reset() is called, or the thread exits.

Throws:

boost::thread_resource_error if an error occurs.

Requires:

cleanup_function(this->get()) does not throw any exceptions.

Effects:

Construct a thread_specific_ptr object for storing a pointer to an object of type T specific to each thread. The supplied cleanup_function will be used to destroy any thread-local objects when reset() is called, or the thread exits.

Throws:

boost::thread_resource_error if an error occurs.

Requires:

All the thread specific instances associated to this thread_specific_ptr (except maybe the one associated to this thread) must be null.

Effects:

Calls this->reset() to clean up the associated value for the current thread, and destroys *this.

Throws:

Nothing.

Remarks:

The requirement is due to the fact that in order to delete all these instances, the implementation should be forced to maintain a list of all the threads having an associated specific ptr, which is against the goal of thread specific data.

[Note] Note

Care needs to be taken to ensure that any threads still running after an instance of boost::thread_specific_ptr has been destroyed do not call any member functions on that instance.

Returns:

The pointer associated with the current thread.

Throws:

Nothing.

[Note] Note

The initial value associated with an instance of boost::thread_specific_ptr is NULL for each thread.

Returns:

this->get()

Throws:

Nothing.

Requires:

this->get is not NULL.

Returns:

*(this->get())

Throws:

Nothing.

Effects:

If this->get()!=new_value and this->get() is non-NULL, invoke delete this->get() or cleanup_function(this->get()) as appropriate. Store new_value as the pointer associated with the current thread.

Postcondition:

this->get()==new_value

Throws:

boost::thread_resource_error if an error occurs.

Effects:

Return this->get() and store NULL as the pointer associated with the current thread without invoking the cleanup function.

Postcondition:

this->get()==0

Throws:

Nothing.


PrevUpHomeNext