std::mutex
std::mutex是C++11中封装的互斥量,直接看源码
class _LIBCPP_TYPE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(capability("mutex")) mutex
{
__libcpp_mutex_t __m_ = _LIBCPP_MUTEX_INITIALIZER;
public:
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR mutex() = default;
//不可拷贝与赋值
mutex(const mutex&) = delete;
mutex& operator=(const mutex&) = delete;
#if defined(_LIBCPP_HAS_TRIVIAL_MUTEX_DESTRUCTION)
~mutex() = default;
#else
~mutex() _NOEXCEPT;
#endif
//上锁
void lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability());
//尝试上锁
bool try_lock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(try_acquire_capability(true));
//解锁
void unlock() _NOEXCEPT _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability());
typedef __libcpp_mutex_t* native_handle_type;
//返回当前互斥量的指针
_LIBCPP_INLINE_VISIBILITY native_handle_type native_handle() {return &__m_;}
};
简单看一下上面的基本api,满足了互斥量的基本使用,下面对一些具体实现进行解读
// Mutex
typedef pthread_mutex_t __libcpp_mutex_t;
#define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
在Unix系统下,这个类的私有成员变量的类型就是 pthread_mutex_t,基本可以看出是对Unix系统下的pthread_mutex_t的操作的基本封装。
对于std::mutex的基本使用如下
std::mutex mtx;
mtx.lock();
//do something.....
mtx.unlock();
std::recursive_mutex
std::recursive_mutex 是 C++11 标准库提供的一种互斥量类型,允许同一个线程多次锁定同一个互斥量,而不会导致死锁。这在某些递归函数或需要多次锁定同一资源的情况下非常有用。
#include <iostream>
#include <thread>
#include <mutex>
std::recursive_mutex mtx;
void recursive_function(int count) {
if (count <= 0) return;
mtx.lock();
std::cout << "Lock acquired, count: " << count << std::endl;
// 递归调用
recursive_function(count - 1);
std::cout << "Unlocking, count: " << count << std::endl;
mtx.unlock();
}
int main() {
std::thread t1(recursive_function, 3);
std::thread t2(recursive_function, 3);
t1.join();
t2.join();
return 0;
}
运行结果为
Lock acquired, count: 3
Lock acquired, count: 2
Lock acquired, count: 1
Unlocking, count: 1
Unlocking, count: 2
Unlocking, count: 3
Lock acquired, count: 3
Lock acquired, count: 2
Lock acquired, count: 1
Unlocking, count: 1
Unlocking, count: 2
Unlocking, count: 3
源码实现
class _LIBCPP_TYPE_VIS recursive_mutex
{
__libcpp_recursive_mutex_t __m_;
public:
recursive_mutex();
~recursive_mutex();
recursive_mutex(const recursive_mutex&) = delete;
recursive_mutex& operator=(const recursive_mutex&) = delete;
void lock();
bool try_lock() _NOEXCEPT;
void unlock() _NOEXCEPT;
typedef __libcpp_recursive_mutex_t* native_handle_type;
_LIBCPP_INLINE_VISIBILITY
native_handle_type native_handle() {return &__m_;}
};
与std::mutex类似,其成员类型为
typedef pthread_mutex_t __libcpp_recursive_mutex_t;
具体api实现无法查看
std::recursive_timed_mutex
std::recursive_timed_mutex 是 C++11 标准库提供的一种互斥量类型,它不仅允许同一个线程多次锁定同一个互斥量,还支持超时功能。也就是说,线程可以尝试在指定的时间内锁定互斥量,如果在超时时间内无法锁定,则返回失败。
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
std::recursive_timed_mutex mtx;
void recursive_function(int count) {
if (count <= 0) return;
// 尝试在 100 毫秒内锁定互斥量
if (mtx.try_lock_for(std::chrono::milliseconds(100))) {
std::cout << "Lock acquired, count: " << count << std::endl;
// 递归调用
recursive_function(count - 1);
std::cout << "Unlocking, count: " << count << std::endl;
mtx.unlock();
} else {
std::cout << "Failed to acquire lock, count: " << count << std::endl;
}
}
int main() {
std::thread t1(recursive_function, 3);
std::thread t2(recursive_function, 3);
t1.join();
t2.join();
return 0;
}
具体实现如下
class _LIBCPP_TYPE_VIS recursive_timed_mutex
{
mutex __m_;
condition_variable __cv_;
size_t __count_;
__thread_id __id_;
public:
recursive_timed_mutex();
~recursive_timed_mutex();
recursive_timed_mutex(const recursive_timed_mutex&) = delete;
recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
void lock();
bool try_lock() _NOEXCEPT;
template <class _Rep, class _Period>
_LIBCPP_INLINE_VISIBILITY
bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
{return try_lock_until(chrono::steady_clock::now() + __d);}
template <class _Clock, class _Duration>
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
void unlock() _NOEXCEPT;
};