Are mutexes assigned to specific regions of memory?

I’m currently reading C++ Concurrency in Action by Anthony Williams and I’m facing an obstacle in thought.

First he describes deadlocks as when two threads lock simultaneously (at least, that’s how I understood it), which makes sense.

However, he goes on to explain how you can lock two mutexes at the same time. Obviously this has a function, but with the above (assumingly wrong) understanding that would instantly deadlock both threads.

From that obstacle arises a new one; obviously it won’t deadlock, which means the mutexes must have a more advanced use than just allowing a single thread to do work in a process. In the book, mutexes are given to particular objects.

This leads me to my overall question(s): Are mutexes assigned to specific memory regions, such as objects? If so, how?

The book does a great job at explaining how mutexes can be used but never really describes what mutexes are at a low level. I realize they’re implementation specific but I never really grasped what exactly they do or how the locking functions use them.

2

A mutex is not a specific memory region; it’s a signal, basically a special type of object, and it works purely “on the honor system,” so to speak. A mutex protects a region of code because calls to it surround the code in question, not because it has any inherent relationship to the code. (Even when it’s a lock associated with an object, like with C#’s lock keyword, locks only work when they’re invoked by some piece of code.)

There are two things that can happen when a piece of code attempts to acquire a lock: either it can find it available and acquire the lock, or it can find it already in use and fail. The most common response to failing is to wait until the lock becomes available. And a deadlock doesn’t happen because two threads are both trying to use a lock at the same time; it happens because two threads are both waiting on each other at the same time.

Imagine that two locks exist, named A and B, and two threads exist, called 1 and 2. Both of them are doing things that require locked access to both A and B, but thread 1 holds A and thread 2 holds B. If they both try to acquire the other lock, and then (as is the norm) attempt to wait forever until it becomes available, they’ll both be stuck waiting forever (deadlocked).

The best way to prevent a situation like this is to identify situations in which your code may need to hold more than one lock at a time, and impose some sort of general ordering rule across the entire codebase, such that no thread can acquire multiple concurrent locks out of order. If no thread that needs A and B can acquire B without already holding A, for example, then it’s impossible to get stuck in the deadlock described above.

At a low level, a mutex can be thought of as containing two pieces of data: a variable identifying the current holder, such as a thread ID number (or 0 if it’s currently unlocked) and some sort of list of threads that are currently waiting on it. Access to this data is controlled by atomic operations, to ensure that multiple threads both attempting to acquire the mutex don’t overwrite or corrupt each other.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật