From: wolf Date: Mon, 13 Jan 2003 19:44:48 +0000 (+0000) Subject: Make the counter variable volatile in MT mode, by using some template magic. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4c8afad007c68378c80efed6b56b5168a57bbeff;p=dealii-svn.git Make the counter variable volatile in MT mode, by using some template magic. git-svn-id: https://svn.dealii.org/trunk@6915 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/base/include/base/subscriptor.h b/deal.II/base/include/base/subscriptor.h index b50538954c..9dd661f3d8 100644 --- a/deal.II/base/include/base/subscriptor.h +++ b/deal.II/base/include/base/subscriptor.h @@ -105,22 +105,90 @@ class Subscriptor private: + /** + * Template class that declares + * in inner typedef with the + * following semantics: if the + * first template parameter is + * @p{true}, then the inner + * typedef is @p{volatile T}, + * otherwise @p{T}. We achieve + * this behavior by partial + * specialization of the general + * template for both values of + * the boolean argument. + * + * This trick is used to declare + * the type of the counter + * variable to be eiter volatile + * or not, depending on whether + * we are in multithreaded mode + * or not. (If we are in MT mode, + * then we need the @p{volatile} + * specifier since more than one + * thread my modify the counter + * at the same time. + * + * Since we only partially + * specialize the case that the + * boolean template argument is + * @p{false}, this general + * template catches the case that + * it is @p{true}, i.e. in a + * sense it is also a + * specialization since a + * @p{bool} can only have two + * states. + * + * @author Wolfgang Bangerth, 2003 + */ + template struct PossiblyVolatile + { + typedef volatile T type; + }; + + /** + * Specialization of the template + * for the case that the first + * template argument is + * @p{false}, i.e. the + * non-volatile case. + */ + template struct PossiblyVolatile + { + typedef T type; + }; + + /** - * Store the number of objects which - * subscribed to this object. Initialally, - * this number is zero, and upon - * destruction it shall be zero again - * (i.e. all objects which subscribed - * should have unsubscribed again). + * Store the number of objects + * which subscribed to this + * object. Initialally, this + * number is zero, and upon + * destruction it shall be zero + * again (i.e. all objects which + * subscribed should have + * unsubscribed again). + * + * The creator (and owner) of an + * object is not counted. * - * The creator (and owner) of an object - * is not counted. + * We use the @p{mutable} keyword + * in order to allow subscription + * to constant objects also. * - * We use the @p{mutable} keyword in order - * to allow subscription to constant - * objects also. + * In multithreaded mode, this + * counter may be modified by + * different threads. We thus + * have to mark it + * @p{volatile}. However, this is + * counter-productive in non-MT + * mode since it may pessimize + * code. So use the above + * template class to selectively + * add volatility. */ - mutable unsigned int counter; + mutable PossiblyVolatile::type counter; /** * Pointer to the typeinfo object