[英]How does a thread acquire a mutex lock?
My question here is about a thread acquiring a mutex lock. 我的问题是关于获取互斥锁的线程。
We use pthread_mutex_t
locks to ensure sequential access to variables, when being read/written in a multi-threaded program. 当在多线程程序中读写时,我们使用
pthread_mutex_t
锁来确保顺序访问变量。 But pthread_mutex_t lock ;
但是
pthread_mutex_t lock ;
is also a global variable shared between threads. 也是线程之间共享的全局变量。 Will accessing of this global variable (the lock) be the same as accessing other global variables?
访问此全局变量(锁)是否与访问其他全局变量相同? If it is the same, then won't we face the same problem that we will face for other global variables?
如果相同,那么我们是否将面对与其他全局变量一样的问题? And if not, then how accessing a lock data structure different from accessing other global variables?
如果不是,那么如何访问不同于访问其他全局变量的锁数据结构?
In short: Why don't we need to lock access to locks themselves and so end up in a chicken-and-egg problem? 简而言之:为什么我们不需要锁定自己对锁的访问权限,而最终遇到鸡与蛋的问题呢?
You don't really access pthread_mutex_t
variables directly, you access them with some given functions ( pthread_mutex_lock
for example). 您实际上并没有直接访问
pthread_mutex_t
变量,而是使用某些给定的函数(例如pthread_mutex_lock
)访问它们。
These functions are built so that there is no concurrent access to these structures, either by the use of some test-and-set instruction ( http://en.wikipedia.org/wiki/Test-and-set ) or by using some atomic portion of code ( http://en.wikipedia.org/wiki/Linearizability ). 建立这些功能的目的是通过使用某些测试设置指令( http://en.wikipedia.org/wiki/Test-and-set )或使用某些测试设置指令,而不会同时访问这些结构。代码的基本部分( http://en.wikipedia.org/wiki/Linearizability )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.