简体   繁体   English

pthread互斥锁解锁为什么同一个线程?

[英]pthread mutex unlock why same thread?

May be a basic question?. 可能是个基本问题?

1) Why the mutex should be unlocked on the same thread ?. 1)为什么互斥锁应该在同一个线程上解锁? Do we have any specific reason? 我们有什么具体的理由吗? 2) Why to keep pthraed_mutex_lock when the same be achieved by sem_wait/sem_post if i understand correctly ? 2)如果我理解正确的话,为什么在sem_wait / sem_post实现同样的情况下保持pthraed_mutex_lock?

A mutex is designed to be extremely fast and lightweight in the most common case. 在最常见的情况下,互斥锁设计为极其快速和轻便。 The most common case is when a thread enters a critical section, does a few quick changes to shared state, and then exits the critical section. 最常见的情况是线程进入临界区,对共享状态进行一些快速更改,然后退出临界区。 If you have more complex requirements, use a more complex synchronization object. 如果您有更复杂的要求,请使用更复​​杂的同步对象。 But typically, a pthread mutex will be lighter than almost any other synchronization primitive. 但通常情况下,pthread互斥体将比几乎任何其他同步原语更轻。

While they can sometimes be used interchangeably, mutexes and semaphores have very different conceptual foundations. 虽然它们有时可以互换使用,但互斥量和信号量具有非常不同的概念基础。 The degree to which their actual behavior differs depends on your particular application. 他们的实际行为的不同程度取决于您的具体应用。

A mutex conceptually has an owner - one thread which, by convention/contract, the programmer treats as the only thread allowed to access the resource(s) which the mutex protects. 互斥体在概念上具有所有者 - 一个线程,按照约定/契约,程序员将其视为允许访问互斥锁保护的资源的唯一线程。 Depending on the type of mutex it is and the implementation, the owner may be purely a formal construct, or may be an actual field stored in the mutex object. 根据互斥体的类型和实现,所有者可以纯粹是正式构造,也可以是存储在互斥对象中的实际字段。 For recursive or error-checking mutexes, for example, storing an owner allows the thread that owns the mutex to obtain additional reference counts on it, or to obtain an error if it tries to re-lock it while the lock is still held, respectively. 例如,对于递归或错误检查互斥锁,存储所有者允许拥有互斥锁的线程获取其上的附加引用计数,或者如果它仍然保持锁定时尝试重新锁定它则获取错误。

A semaphore, on the other hand, is fundamentally a counter. 另一方面,信号量基本上是一个反制者。 Waiting on a semaphore is not necessarily tied to obtaining exclusive rights to using a resource, although of course that's one potential application (a semaphore which only takes on the values 0 and 1 can be used as a mutex). 等待信号量并不一定与获得使用资源的专有权相关联,尽管当然这是一个潜在的应用程序(仅采用值0和1的信号量可以用作互斥体)。 Semaphores can be used for many other applications, like waiting/signaling - for example, one thread could loop waiting on a semaphore N times to wait for N threads to post to it, where each thread posts when it finishes a task. 信号量可以用于许多其他应用程序,例如等待/信令 - 例如,一个线程可以循环等待信号量N次以等待N线程发布到它,其中每个线程在完成任务时发布。 In fact, a synchronization object equivalent to condition variables can also be implemented in terms of semaphores. 实际上,也可以用信号量来实现等同于条件变量的同步对象。 They can also be used simply as a portable atomic counter (posting to increment the counter), among many other uses. 它们还可以简单地用作便携式原子计数器(用于递增计数器的过帐),以及许多其他用途。 The "classic" use of a semaphore is to represent the number of available resources from among N equivalent resources, and facilitate waiting when no resources are available, but personally I don't think I've ever used semaphores quite like that. 信号量的“经典”用法是表示N等价资源中可用资源的数量,并且在没有资源可用时便于等待,但我个人认为我从未使用过这样的信号量。

Now if you want to get into the specifics of POSIX mutexes and semaphores, for the most part semaphores are much more powerful. 现在,如果你想了解POSIX互斥量和信号量的细节,大多数情况下信号量更强大。 Not only can they be used for signalling conditions; 它们不仅可用于信号传输条件; the post operation is also async-signal-safe meaning you can use it without restriction from inside signal handlers. post操作也是异步信号安全的,这意味着您可以在不受内部信号处理程序限制的情况下使用它。 On the other hand, mutexes do have one feature which cannot be implemented in terms of semaphores: the robust mutex attribute, which allows you to create mutexes that allow other threads/processes to detect and recover when a thread/process terminated while holding the mutex (rather than just deadlocking). 另一方面,互斥体确实有一个功能无法实现信号量: 强大的互斥锁属性,它允许您创建互斥锁,允许其他线程/进程在线程/进程在保持互斥锁时终止时进行检测和恢复(而不仅仅是死锁)。

Because that's what a mutual exclusion semaphore is for, mutual exclusion (excluding all other threads except this one). 因为这是互斥信号量的用途,互斥(除了这个以外的所有其他线程除外)。

It's to lock a resource so that a particular thread of execution has unfettered access to it. 它是锁定资源,以便特定的执行线程可以自由地访问它。

"Regular" semaphores (as in sem_wait/sem_post ) are counted semaphores. “常规”信号量(如sem_wait/sem_post )是计数信号量。 You can specify that there are N resources available rather than just one as per mutual exclusion. 您可以指定有N资源可用,而不是根据互斥而只有一个资源。 You can certainly emulate mutual exclusion semaphores with the regular variety but you lose some of the protections (such as ensuring only the owner can unlock it). 你当然可以模仿常规变种的互斥信号量但你失去了一些保护措施(例如确保只有主人可以解锁它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM