|
Boost Users : |
From: James E Taylor (james_at_[hidden])
Date: 2005-08-18 05:27:19
Thanks, Pablo,
I'll have a look at it.
James
> FYI Matthew Wilson gave a technique (in "Imperfect C++" for handling
> this,
> by turning the mutex initialization into an integer initialization (that
> doesn't have a problem with [multiple] simultaneous threads entering the
> function)
>
> It uses STLSoft's spin_mutex, and looks pretty much like what you have
> already:
>
> <code>
> #include <platformstl/spin_mutex.hpp>
> #include <stlsoft/lock_scope.hpp>
> using platformstl::spin_mutex;
> using stlsoft::lock_scope;
>
> template<typename T>
> boost::shared_ptr<T>
> my_func()
> {
> // static boost::mutex m;
> // boost::mutex::scoped_lock l(m);
> static int spin_count = 0; // No race condition initializing an
> int
> spin_mutex mutex(&spin_count); // Build the mutex on top of the
> static
> int
> lock_scope lock(mutex);
>
> static boost::shared_ptr<T> p(new T);
> return p;
> }
> </code>
>
> This assumes, of course, you can (and are willing to) use STLSoft...
>
>
> HTH,
> Pablo
___________________________________
NOCC, http://nocc.sourceforge.net
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net