* Depending on the use of threads, we will have to make some variables
* volatile. We do this here in a very old-fashioned C-style, but still
* convenient way.
+ *
+ * @deprecated This macro is deprecated in favor of using the
+ * <code>std::atomic</code> template class.
*/
#ifdef DEAL_II_WITH_THREADS
# define DEAL_VOLATILE volatile
#include <deal.II/base/config.h>
#include <deal.II/base/exceptions.h>
-#include <typeinfo>
+#include <atomic>
#include <map>
#include <string>
+#include <typeinfo>
DEAL_II_NAMESPACE_OPEN
* We use the <tt>mutable</tt> 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 <tt>volatile</tt>. However, this is counter-
- * productive in non-MT mode since it may pessimize code. So use the macro
- * defined in <tt>deal.II/base/config.h</tt> to selectively add volatility.
+ * This counter may be read from and written to concurrently in
+ * multithreaded code: hence we use the <code>std::atomic</code> class
+ * template.
+ *
*/
- mutable DEAL_VOLATILE unsigned int counter;
+ mutable std::atomic<unsigned int> counter;
/**
* In this map, we count subscriptions for each different identification
DEAL_II_NAMESPACE_OPEN
-#ifdef DEBUG
-namespace
-{
-// create a lock that might be used to control subscription to and
-// unsubscription from objects, as that might happen in parallel.
-// since it should happen rather seldom that several threads try to
-// operate on different objects at the same time (the usual case is
-// that they subscribe to the same object right after thread
-// creation), a global lock should be sufficient, rather than one that
-// operates on a per-object base (in which case we would have to
-// include the huge <thread_management.h> file into the
-// <subscriptor.h> file).
- Threads::Mutex subscription_lock;
-}
-#endif
-
-
static const char *unknown_subscriber = "unknown subscriber";
#ifdef DEBUG
if (object_info == nullptr)
object_info = &typeid(*this);
- Threads::Mutex::ScopedLock lock (subscription_lock);
++counter;
# ifndef DEAL_II_WITH_THREADS
if (counter == 0)
return;
- Threads::Mutex::ScopedLock lock (subscription_lock);
--counter;
# ifndef DEAL_II_WITH_THREADS
#include <deal.II/base/thread_management.h>
-#include <cerrno>
+#include <atomic>
#include <cstdlib>
#include <iostream>
-#include <list>
#ifdef DEAL_II_HAVE_UNISTD_H
# include <unistd.h>
{
namespace internal
{
- // counter and access mutex for the
- // number of threads
- volatile unsigned int n_existing_threads_counter = 1;
- Mutex n_existing_threads_mutex;
+ static std::atomic<unsigned int> n_existing_threads_counter(1);
void register_thread ()
{
- Mutex::ScopedLock lock (n_existing_threads_mutex);
++n_existing_threads_counter;
}
void deregister_thread ()
{
- Mutex::ScopedLock lock (n_existing_threads_mutex);
--n_existing_threads_counter;
Assert (n_existing_threads_counter >= 1,
ExcInternalError());
unsigned int n_existing_threads ()
{
- Mutex::ScopedLock lock (internal::n_existing_threads_mutex);
return internal::n_existing_threads_counter;
}
// verify that thread local storage works as advertised
#include "../tests.h"
+#include <atomic>
#include <iomanip>
#include <fstream>
Threads::ThreadLocalStorage<X> *tls_data;
-volatile int counter = 0;
+static std::atomic<int> counter(0);
void execute (int i)
{
// the initialization with an exemplar
#include "../tests.h"
+#include <atomic>
#include <iomanip>
#include <fstream>
Threads::ThreadLocalStorage<X> *tls_data;
-volatile int counter = 0;
+static std::atomic<int> counter(0);
void execute (int i)
{
// see if we can detach from threads
#include "../tests.h"
+#include <atomic>
#include <iomanip>
#include <fstream>
#include <unistd.h>
Threads::Mutex mutex;
-volatile int spin_lock = 0;
+static std::atomic<int> spin_lock(0);
void worker ()
// and makes sure nobody writes into it at undue times
#include "../tests.h"
+#include <atomic>
#include <iomanip>
#include <fstream>
#include <unistd.h>
Threads::Mutex mutex;
-volatile int spin_lock = 0;
+static std::atomic<int> spin_lock(0);
int worker ()