What does std::thread.join() do?

You still need mutexes and conditions. Joining a thread makes one thread of execution wait for another thread to finish running. You still need mutexes to protect shared resources. It allows main() in this example to wait for all threads to finish before quitting itself. #include <iostream> #include <thread> #include <chrono> #include <mutex> using namespace … Read more

Monitor vs Mutex

Since you haven’t specified which OS or language/library you are talking about, let me answer in a generic way. Conceptually they are the same. But usually they are implemented slightly differently Monitor Usually, the implementation of monitors is faster/light-weight, since it is designed for multi-threaded synchronization within the same process. Also, usually, it is provided … Read more

Plural form of word “mutex” [closed]

From a purely linguistics point of view, the correct usage is mutexes because the word mutex is not Latin in origin. Prescriptivists would wail in anguish if mutices were to enter regular usage. The -ices usage (e.g., the plurals of index and vertex) is falling out of favor. Indexes and vertexes are both correct usage, … Read more

Do I need a mutex for reading?

It depends. The C++ language says nothing about threads or atomicity. But on most modern CPU’s, reading an integer is an atomic operation, which means that you will always read a consistent value, even without a mutex. However, without a mutex, or some other form of synchronization, the compiler and CPU are free to reorder … Read more

How do you declare a recursive mutex with POSIX threads?

The code from Michael Foukarakis is almost good but he initializes the mutex twice which leads to undefined behavior. It should just be: pthread_mutex_t Mutex; pthread_mutexattr_t Attr; pthread_mutexattr_init(&Attr); pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&Mutex, &Attr); I actually use this code in production, and I know it works correctly on Linux, Solaris, HP-UX, AIX, Mac OSX and FreeBSD. You … Read more

C++11 equivalent to boost shared_mutex

I tried but failed to get shared_mutex into C++11. It has been proposed for a future standard. The proposal is here. Edit: A revised version (N3659) was accepted for C++14. Here is an implementation: http://howardhinnant.github.io/shared_mutex http://howardhinnant.github.io/shared_mutex.cpp

Synchronising twice on the same object?

Reentrant Synchronized blocks use reentrant locks, which means if the thread already holds the lock, it can re-aquire it without problems. Therefore your code will work as you expect. See the bottom of the Java Tutorial page Intrinsic Locks and Synchronization. To quote as of 2015-01… Reentrant Synchronization Recall that a thread cannot acquire a … Read more

Why doesn’t Mutex get released when disposed?

The documentation explains (in the “Remarks” section) that there is a conceptual difference between instantiating a Mutex object (which does not, in fact, do anything special as far as synchronization goes) and acquiring a Mutex (using WaitOne). Note that: WaitOne returns a boolean, meaning that acquiring a Mutex can fail (timeout) and both cases must … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)