#include <string>
#include <typeinfo>
+#ifdef DEAL_II_WITH_THREADS
+#include <mutex>
+#endif
+
DEAL_II_NAMESPACE_OPEN
/**
unsigned int
n_subscriptions() const;
+ /**
+ * List the subscribers to the input @p stream.
+ */
+ template <typename StreamType>
+ void list_subscribers (StreamType &stream) const;
+
/**
* List the subscribers to @p deallog.
*/
*/
void
check_no_subscribers() const noexcept;
+
+#ifdef DEAL_II_WITH_THREADS
+
+ /**
+ * A mutex used to ensure data consistency when printing out the list
+ * of subscribers.
+ */
+ static std::mutex mutex;
+
+#endif
};
//---------------------------------------------------------------------------
// documentation of this function
}
+template <typename StreamType>
+inline
+void
+Subscriptor::list_subscribers(StreamType &stream) const
+{
+#ifdef DEAL_II_WITH_THREADS
+ std::lock_guard<std::mutex> lock(mutex);
+#endif
+
+ for (map_iterator it = counter_map.begin();
+ it != counter_map.end(); ++it)
+ stream
+ << it->second << '/'
+ << counter << " subscriptions from \""
+ << it->first << '\"'
+ << std::endl;
+}
+
DEAL_II_NAMESPACE_CLOSE
#endif
static const char *unknown_subscriber = "unknown subscriber";
+#ifdef DEAL_II_WITH_THREADS
+std::mutex Subscriptor::mutex;
+#endif
+
+
Subscriptor::Subscriptor()
: counter(0)
, object_info(nullptr)
object_info = &typeid(*this);
++counter;
- // This feature is disabled when we compile with threads: see the
- // documentation of this class.
-# ifndef DEAL_II_WITH_THREADS
+#ifdef DEAL_II_WITH_THREADS
+ std::lock_guard<std::mutex> lock(mutex);
+#endif
+
const char *const name = (id != 0) ? id : unknown_subscriber;
map_iterator it = counter_map.find(name);
else
it->second++;
-# else
- (void)id;
-# endif
#else
(void)id;
#endif
--counter;
- // This feature is disabled when we compile with threads: see the
- // documentation of this class.
-# ifndef DEAL_II_WITH_THREADS
+#ifdef DEAL_II_WITH_THREADS
+ std::lock_guard<std::mutex> lock(mutex);
+#endif
+
map_iterator it = counter_map.find(name);
AssertNothrow(it != counter_map.end(),
ExcNoSubscriber(object_info->name(), name));
AssertNothrow(it->second > 0, ExcNoSubscriber(object_info->name(), name));
it->second--;
-# endif
#else
(void)id;
#endif
void
Subscriptor::list_subscribers() const
{
-#ifndef DEAL_II_WITH_THREADS
- for (map_iterator it = counter_map.begin(); it != counter_map.end(); ++it)
- deallog << it->second << '/' << counter << " subscriptions from \""
- << it->first << '\"' << std::endl;
-#else
- deallog << "No subscriber listing with multithreading" << std::endl;
-#endif
+ list_subscribers(deallog);
}
DEAL_II_NAMESPACE_CLOSE