Reputation: 1057
Can someone please explain in what scenarios it would be beneficial to use std::mutex
vs. pthread_mutex_t
. I don't understand why we would ever use pthread_mutex_t
.
Upvotes: 12
Views: 14038
Reputation: 19761
std::mutex is just a thin wrapper around pthread_mutex on systems supporting pthreads.
In general, the operations on the std:: thread primitives are quite limited vs the native versions (pthreads or windows threads). If you don't need those features, you should always use the std:: versions, but if you do need the advanced features, then you have no choice but to use the native version.
native handle()
method exists for exactly this reason.
Upvotes: 10
Reputation: 976
See pthread_mutexattr for an idea of the other flavors of POSIX mutexes available besides the default. Though one of the main ones-- recursive vs. nonrecursive-- is available with std::recursive_mutex, this is not true for things like BSD priority ceilings, etc.
Upvotes: 0
Reputation: 10316
The pthread_mutex_t
is a POSIX solution (available for linux and other UNIX systems) that existed before c++11 introduced synchronisation primitives into the c++ library. You should use std::mutex
now, amongst other things it is more cross-platform (can be used under Windows also).
Upvotes: 16
Reputation: 49976
std::mutex
is from standard library, so if you use it your code will compile also on platforms where pthreads are not provided. For example under windows std::mutex
uses native WinAPI mutex implementation.
Its best to always use std::mutex
.
Upvotes: 1