Re: [Boost-users] Boost-users Digest, Vol 637, Issue 1
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
participants (1)
-
James E Taylor