Tôi rất bối rối về sự khác biệt giữa khóa và mutex. Trong tài liệu Boost, nó nói,Sự khác nhau giữa "mutex" và "lock" là gì?
Khóa các loại
- Lớp mẫu lock_guard
- Lớp mẫu unique_lock
- Lớp mẫu shared_lock
- Lớp mẫu upgrade_lock
- Lớp mẫu upgrade_to_unique_lock
- Mutex-class class scoped_tr y_lock
loại Mutex
- Lớp mutex
- typedef try_mutex
- Lớp timed_mutex
- Lớp recursive_mutex
- typedef recursive_try_mutex
- Lớp recursive_timed_mutex
- Lớp shared_mutex
Trong một bài viết, tôi thấy các chức năng như thế này,
boost::shared_mutex _access;
void reader()
{
boost::shared_lock<boost::shared_mutex> lock(_access);
// do work here, without anyone having exclusive access
}
void conditional_writer()
{
boost::upgrade_lock<boost::shared_mutex> lock(_access);
// do work here, without anyone having exclusive access
if (something) {
boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);
// do work here, but now you have exclusive access
}
// do more work here, without anyone having exclusive access
}
Cập nhật câu hỏi
- bất cứ ai có thể cung cấp một số làm rõ giữa "mutex" và " Khóa"?
- Bạn có cần tạo một shared_lock cho số shared_mutex? Điều gì sẽ xảy ra nếu tôi tạo một unique_lock cho một số shared_mutex?
- Hoặc nếu tôi tạo ra một shared_lock cho một mutex, có nghĩa là các mutex thể không được chia sẻ giữa nhiều chủ đề?
Bạn có thể tìm thấy bài viết của tôi "Cách hoạt động của mutex?" cũng hữu ích: http://mortoray.wordpress.com/2011/12/16/how-does-a-mutex-work-what-does-it-cost/ –
Đó là những câu hỏi mới. Bạn nên hỏi họ trong một câu hỏi mới. Bạn không thêm theo dõi cho câu hỏi đã tồn tại. Ngoài ra, công cụ này là tất cả có trong tài liệu của Boost. –