+ 1
Why thread locks are templated
Hi. We have unique_lock, Lock guard and just wondering why they are templated class ? We generally have mutex as template parameters. What else could be used with these locks ?
2 ответов
+ 2
Because they are templated,std::unique_lock and std::lock_guard can function with a variety of synchronization primitives. They can operate with additional kinds of mutex-like objects in addition to standard::mutex by creating them as templates.
Typical types for template parameters include: mutex
the typical mutex type used for simple locking.
Recursive_mutex std::
permits numerous acquisitions of the mutex by the same thread.
Std::timed_mutex
provides more timed locking functionality.
Recursive_timed_mutex std::
a recursive and timed mutex combo.
Implementations of custom mutexes
Using unique_lock or lock_guard, you can create your own mutex class that complies with the Lockable or TimedLockable principles.
The templated design allows:
1. Generic locking mechanisms
2. Extensibility
3. Code Reusability
0
Thanks Sanjai.
Just curious to know why someone would choose to have custom mutex? Is mutex not enough in some scenarios (For Example)?