What difference between C++ std::mutex and windows CreateMutex - multithreading

I'm programming in Windows on c++ (Visual Studio)
I can create mutex using either std::mutex or CreateMutex.
What is the difference between them? Which one I should prefer and which one is faster? Do they have any specifics in usage or implimintation? or maybe std::mutex is just a shell and uses CreateMutex inside?

Besides the fact that std::mutex is cross platform and CreateMutex is not another difference is that the WinAPI mutexes (created through CreateMutex) can be used for synchronization between different processes, while std::mutex can not be used for that. In that sense std::mutex is more equal to the WinAPI Critical Section.
However there are also other things to consider, e.g. if you need to interoperate with std::condition_variable's or with WinAPI events (e.g. in order to use WaitForMultipleObjects).

std::mutex is provided by the C++ standard library and yes, most probably it will call CreateMutex under the hood.
Since CreateMutex is not portable, std::mutex is generally preferred.

A difference in behavior is that CreateMutex under Windows creates a recursive mutex, while std::mutex is a non-recursive one. You'd have to use std::recursive_mutex that was also added in C++11.

Related

The advantage of c++11 threads

Threads are added to C++11 language
Then I am wondering what is the difference, advantages and impact?
If this code is by c++03
#include <iostream>
#include <pthread.h>
void *call_from_thread(void *)
{
std::cout << "Launched by thread" << std::endl;
return NULL;
}
int main()
{
pthread_t t;
pthread_create(&t, NULL, call_from_thread, NULL);
pthread_join(t, NULL);
return 0;
}
and this one by c++11
#include <iostream>
#include <thread>
void call_from_thread()
{
std::cout << "Hello, World" << std::endl;
}
int main()
{
std::thread t1(call_from_thread);
t1.join();
return 0;
}
Then, I see no fundamental advantage.
Also, when it is said a part of language, I am confused about that as I see no new keyword or no new syntax. I just see a new standard library. Is it beyond that? and is this just a paraphrase of pthread?
Besides being much more portable, C++11 threads also provides other benefits:
allows passing arguments (and more than one) to the thread handler in a type safe way. pthread_create passes a single void*, whereas with std::thread you get compile time errors if something is wrong instead of runtime errors
the thread handler can be a lambda
a std::thread is an object, not a pointer, which makes managing object lifetimes easier, and reduces risk of dangling pointers, especially if combined with std::unique_ptr or std::shared_ptr if pointer juggling is even needed.
Those are the immediate benefits that come to mind.
As for standard library vs language spec: they are both part of the same standard, so they are both considered "C++11". Note that std::thread can not be implemented in C++03, since move semantics is new in C++11 and std::thread implements move.
The primary advantage of C++ thread library is portability. Like many other C++ standard library facilities platform-dependent libraries like pthreads or Win32API provide more control over your threads comparing to C++ thread library. For instance on Windows Win32 API thread library allows you to set thread stack size you can't do with C++ thread library without using platform dependent code. API functions like TerminateThread allows developers to terminate their running threads (a very dangerous operation) or setting thread priority using function SetThreadPriority.
But, using C++ thread library makes your code platform independent. And it's not just about class thread. There are other facilities like mutexes, conditional variables, locks that have been standardized so every C++ implementation is supposed to implement them to comply with C++ standard.
So, using C++ thread library is always a trade-off. You are losing some degree of control over threads but your code is being portable. And if you really need some low level feature, you can use std::thread::native_handle that allows you to mix standard and platform dependent code.
This link std::thread::native_handle provides a nice example how to mix class thread and pthread library.
It sounds funny in 2019, but one small disadvantage of std::thread (since you asked about it) is ~130kb of code added to your binary.

C++11 equivalent of Windows SRWLock

I'm trying to implement my own read/write lock using atomic types. I can easily define exclusive locks, but I fail to create locks for shared reader threads, like SRWLock does (see SRWLock). My question is how to implement locks that can be used in exclusive mode (one reader/writer threads at a time) or in shared mode (multiple reader threads at a time).
I can't use std::mutex lock because it doesn't support multiple readers. Also I don't use boost, so no shared_mutex either.
The shared timed mutex
There is no equivalent for that kind of read-write locking in the C++11 standard library. The good news is that there is one in C++14 and it's called shared_timed_mutex.
Take a look here:
http://en.cppreference.com/w/cpp/thread/shared_timed_mutex
Compiler support
GCC's recent versions support shared_timed_mutex according to its documentation if you use the -std=c++14 compiler flag. The bad news is that Visual C++ doesn't support it yet, or at least I haven't been able to find any concrete info about it, the closest thing I got is this feature table which says that Shared Locking in C++ is missing.
Possible alternatives
You can implement this kind of thing using a mutex and a semaphore as described in this tutorial if you use a library that has these primitives.
If you prefer to stay with the standard library, you can implement the stuff with an std::mutex and an std::condition_variable similarly to how it's done here or here.
There is also shared_mutex in boost (as you already noted), or uv_rwlock_t in libuv, or pthread_rwlock in unix-like OSes.

volatile keyword for objects in C++

I have a thread safe counter object( it's a class which uses std::atomic load() and store() )
as one of the class members. Thread 1 increments the counter and Thread 2 reads the counter.
Usually, primitive types ( int etc ) which are shared by different threads are declared volatile to prevent any compiler optimizations. Do I have to declare this thread safe counter object which is shared by 2 different threads as volatile ?
Could someone provide more insight into this ?
No. There is no need if the object is declared atomic.
A C or C++ compiler may not reorder reads and writes to volatile memory locations, nor may it omit a read or write to a volatile memory location.
By using atomic, it already achieves the what volatile intended to do, so no need to declare volatile.
Take a look at: volatile (C++) msdn article
You don't have to because
"The volatile keyword in C++11 ISO Standard code is to be used only for hardware access; do not use it for inter-thread communication. For inter-thread communication, use mechanisms such as std::atomic from the C++ Standard Template Library."

Win32 alternative for the destructor in pthread_keycreate (when I cannot control dllmain)

In pthreads, you can associate a destructor function with each per-thread storage slot. When a thread dies, if the slot is non-0, the destructor is called.
In a Win32 DLL, the DLLMain function, called at thread exit, can do the same thing.
What can I do in code that lives in a purely static library?
This is a hard problem, and requires sticking callbacks in special locations. Luckily for you, it is solved in Boost.Thread. Use boost::this_thread::at_thread_exit, or boost::thread_specific_ptr
Windows support Thread Local Storage (TLS) in a DLL. It can be very practical if you want have some memory blocks per thread with the unique value (unique per thread). Inside of any other function from the DLL you can get the value which correspond to the current thread in very easy way. It is very useful in some scenarios. Look at here for more details.
I dun't use pthreads myself, but I suppose that per-thread storage slot introduced to make the work with TLS more comfortable.
UPDATED: From your comment I see that you misunderstand my answer. I'm not a POSIX developer, I develop in Win32 only and your question is about WIn32 API possibilities of per-thread allocation and deallocations. I try to explain the possibilities and you can decide yourself which one are better for your specific scenarios.
The equivalent of pthread_XXX functions in Win32 are following:
pthread_key_create TlsAlloc
pthread_setspecific TlsSetValue
pthread_getspecific TlsGetValue
pthread_key_delete TlsFree
I not recommended you to use the construct __declspec(thread), which is more compiler specific.
The example Using Thread Local Storage shows how to use thread local storage (TLS) without DLLs, but I personally like and use TLS only in DLL.
The destructor parameter of the pthread_key_create function has no analog in Win32, but I don't see here any problem. All C/C++ compilers support __try {/**/} __finally {/**/} construct of the Structured Exception Handling so you can use it in the body of your thread function and implement in the way any Cleaning up Resources exacly like you can do this in the main thread.
I find pity that you not included in your question an example which shows how you typically use destructor of the pthread_key_create function. I find that examples can clear much things without a lot of words. So if my answer do not help you we can better explain all in examples: you write an example and probably short comment what it should do and I could write the same code using Win32 API only in C or C++.

How can I implement java-like synchronization (monitors) using the Win32 API?

Each Java object (and its class) has an associated monitor. In pthread terms a Java monitor is equivalent to the combination of a reentrant mutex and a condition variable.
For locking, the Win32 API provides Mutex objects (which are reentrant but heavyweight) and Critical Sections (which are non-reentrant but lightweight). It also provides other synchronization constructs such as Semaphores and Events but has no explicit concept of a condition variable.
If I were writing a JVM, how could I use these Win32 concepts to implement Java monitors?
Windows has SignalObjectAndWait() which can be used very much like a wait on a condition variable in a monitor. You can use an Event (that is Reset) and a Mutex and then use PulseEvent() to do the equivalent of signalling the condition variable.
I suggest you take a look at the OpenJDK source to see how the class ReentrantLock was implemented.
(I haven't checked it myself so i'm not sure of the answer).
the util.concurrent locks are implemented using native API.

Resources