pthread_mutex_lock()和pthread_mutex_unlock()做什么?

发布于 2025-01-25 20:28:14 字数 136 浏览 3 评论 0原文

pthread_mutex_lock()pthread_mutex_unlock()函数真的做什么。我知道锁定是使代码被阻止的,直到再次解锁为止。我仍然对在锁定和解锁的那个时期之间发生的事情感到困惑。

What do the pthread_mutex_lock() and pthread_mutex_unlock() functions really do. I understand that the lock makes it so that the code is blocked until it is unlocked again. I'm still confused on what happens in between that period where it is locked and unlocked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘你却要生生世世 2025-02-01 20:28:14

在C中,这是锁定和解锁静音的基本方法,

    int pthread_mutex_lock(pthread_mutex_t *mutex);
    int pthread_mutex_unlock(pthread_mutex_t *mutex);

基本上是pthread_mutex_unlock()将停止锁定并等到程序要求发生另一个锁。

In C, this is the basic way to lock and unlock mutexes is

    int pthread_mutex_lock(pthread_mutex_t *mutex);
    int pthread_mutex_unlock(pthread_mutex_t *mutex);

Basically pthread_mutex_unlock() will stop the lock and wait until the program asks for another lock to happen.

听风吹 2025-02-01 20:28:14

在最基本的情况下,每个硬件都有某种指令或一系列指令进行“测试和设置”:“作为一种原子操作,测试此变量是否具有值x,如果是的,则将其设置为y。我知道我是否成功。”

因此,我们有:

mutex_var = 0

pthread_mutex_lock(&mutex_var):
while True:
    if successful in atomically changing mutex_var from 0 to 1:
        break

pthread_mutex_unock(&mutex_var):
    mutex_var = 0

现在,该代码有两个基本问题:

  1. 任何人都可以解锁互斥X,即使他们不拥有它,
  2. 也可以浪费CPU。等待锁的线是在无限的循环中:“我可以吗?” “我可以吗?” “我可以吗?”

实际实施情况会做更多。 mutex包含有关当前拥有的互联克人(如果被锁定)的更多信息,以便只有该线程才能解锁。

因此,实际代码更像:

pthread_mutex_lock():
while True:
     if successful in atomically changing mutex_var from 0 to 1:
         break
     go to sleep waiting on this mutex

pthread_mutex_unlock():
     verify that this thread owns the mutex
     set mutex_var back to 0
     wake up all threads waiting on this mutex    

At the most basic, every hardware out there has some sort of instruction or sequence of instructions that does "test and set": "As an atomic action, test if this variable has value x, and if so, set it to y. Let me know if I am successful".

So at it's heart, we have:

mutex_var = 0

pthread_mutex_lock(&mutex_var):
while True:
    if successful in atomically changing mutex_var from 0 to 1:
        break

pthread_mutex_unock(&mutex_var):
    mutex_var = 0

Now this code has two basic problems:

  1. Anyone can unlock the mutex, even if they don't own it
  2. This is wasteful of CPU. A thread waiting for the lock is in an infinite loop: "Can I have it?" "Can I have it?" "Can I have it?"

Actual implementations do a bit more. The mutex contains more information about who currently owns the mutex, if it is locked, so that only that thread can unlock it.

So the actual code is more like:

pthread_mutex_lock():
while True:
     if successful in atomically changing mutex_var from 0 to 1:
         break
     go to sleep waiting on this mutex

pthread_mutex_unlock():
     verify that this thread owns the mutex
     set mutex_var back to 0
     wake up all threads waiting on this mutex    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文