The single and important difference is that std::scoped_lock
has a variadic constructor taking more than one mutex. This allows to lock multiple mutexes in a deadlock avoiding way as if std::lock
waswere used.
{
// safely locked as if using std::lock
std::scoped_lock<std::mutex, std::mutex> lock(mutex1, mutex2);
}
Previously you had to do a little dance to lock multiple mutexes in a safe way using std::lock
as explained this answer.
The addition of scope lock makes this easier to use and avoids the related errors. You can consider std::lock_guard
deprecated. The single argument case of std::scoped_lock
can be implemented as a specialization and such you don't have to fear about possible performance issues.
GCC 7 already has support for std::scoped_lock
which can be seen here.
For more information you might want to read the standard paper