Linux Pthread互斥锁由不同线程解锁
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5454746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pthread Mutex lock unlock by different threads
提问by codingfreak
A Naive question ..
一个天真的问题..
I read before saying - "A MUTEX has to be unlocked only by the thread that locked it."
我之前读过说 - “一个 MUTEX 只能被锁定它的线程解锁。”
But I have written a program where THREAD1locks mutexVar and goes for a sleep. Then THREAD2can directly unlock mutexVar do some operations and return.
但是我写了一个程序,其中THREAD1锁定 mutexVar 并进入睡眠状态。然后THREAD2可以直接解锁 mutexVar 做一些操作并返回。
==> I know everyone say why I am doing so ?? But my question is - Is this a right behaviour of MUTEX ??
==>我知道每个人都说我为什么这样做??但我的问题是 - 这是 MUTEX 的正确行为吗??
==> Adding the sample code
==> 添加示例代码
void *functionC()
{
pthread_mutex_lock( &mutex1 );
counter++;
sleep(10);
printf("Thread01: Counter value: %d\n",counter);
pthread_mutex_unlock( &mutex1 );
}
void *functionD()
{
pthread_mutex_unlock( &mutex1 );
pthread_mutex_lock( &mutex1 );
counter=10;
printf("Counter value: %d\n",counter);
}
int main()
{
int rc1, rc2;
pthread_t thread1, thread2;
if(pthread_mutex_init(&mutex1, NULL))
printf("Error while using pthread_mutex_init\n");
if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) )
{
printf("Thread creation failed: %d\n", rc1);
}
if( (rc2=pthread_create( &thread2, NULL, &functionD, NULL)) )
{
printf("Thread creation failed: %d\n", rc2);
}
采纳答案by Ernest Friedman-Hill
What you've done is simply not legal, and the behavior is undefined. Mutexes only exclude threads that play by the rules. If you tried to lock mutex1 from thread 2, the thread would be blocked, of course; that's the required thing to do. There's nothing in the spec that says what happens if you try to unlock a mutex you don't own!
你所做的根本不合法,行为是未定义的。互斥体仅排除按规则运行的线程。如果您试图从线程 2 中锁定 mutex1,当然该线程将被阻塞;这是必须要做的事情。规范中没有任何内容说明如果您尝试解锁不属于您的互斥锁会发生什么!
回答by Duck
Pthreads has 3 different kinds of mutexes: Fast mutex, recursive mutex, and error checking mutex. You used a fast mutex which, for performance reasons, will not check for this error. If you use the error checking mutex on Linux you will find you get the results you expect.
Pthreads 有 3 种不同类型的互斥锁:快速互斥锁、递归互斥锁和错误检查互斥锁。您使用了快速互斥锁,出于性能原因,它不会检查此错误。如果您在 Linux 上使用错误检查互斥锁,您会发现您得到了预期的结果。
Below is a small hack of your program as an example and proof. It locks the mutex in main() and the unlock in the created thread will fail.
以下是您程序的一个小技巧,作为示例和证明。它锁定 main() 中的互斥锁,并且在创建的线程中解锁将失败。
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
/*** NOTE THE ATTR INITIALIZER HERE! ***/
pthread_mutex_t mutex1 = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
int counter = 0;
void *functionD(void* data)
{
int rc;
if ((rc = pthread_mutex_unlock(&mutex1)) != 0)
{
errno = rc;
perror("other thread unlock result");
exit(1);
}
pthread_mutex_lock(&mutex1);
counter=10;
printf("Thread02: Counter value: %d\n",counter);
return(data);
}
int main(int argc, char *argv[])
{
int rc1;
pthread_t thread1;
if ((rc1 = pthread_mutex_lock(&mutex1)) != 0)
{
errno = rc1;
perror("main lock result");
}
if( (rc1 = pthread_create(&thread1, NULL, &functionD, NULL)))
{
printf("Thread creation failed: %d\n", rc1);
}
pthread_join(thread1, NULL);
}
回答by johnnycrash
A mutex is used to prevent multiple threads from executing code that is only safe for one thread at a time.
互斥锁用于防止多个线程执行一次仅对一个线程安全的代码。
To do this a mutex has several features:
要做到这一点,互斥锁有几个特点:
A mutex can handle the race conditions associated with multiple threads trying to "lock" the mutex at the same time and always results with one thread winning the race.
Any thread that loses the race gets put to sleep permanently until the mutex is unlocked. The mutex maintains a list of these threads.
A will hand the "lock" to one and only one of the waiting threads when the mutex is unlocked by the thread who was just using it. The mutex will wake that thread.
互斥锁可以处理与试图同时“锁定”互斥锁的多个线程相关的竞争条件,并且总是导致一个线程赢得竞争。
任何失去竞争的线程都会永久休眠,直到互斥锁被解锁。互斥锁维护这些线程的列表。
当互斥锁被刚刚使用它的线程解锁时,A 会将“锁”交给一个且只有一个等待线程。互斥将唤醒该线程。
If that type of pattern is useful for some other purpose then go ahead and use it for a different reason.
如果这种类型的模式对某些其他目的有用,那么请继续并出于其他原因使用它。
Back to your question. Lets say you were protecting some code from multiple thread accesses with a mutex and lets say 5 threads were waiting while thread A was executing the code. If thread B (not one of the ones waiting since they are permanently slept at the moment) unlocks the mutex, another thread will commence executing the code at the same time as thread A. Probably not desired.
回到你的问题。假设您正在使用互斥锁保护一些代码免受多线程访问,并且假设线程 A 正在执行代码时有 5 个线程正在等待。如果线程 B(不是正在等待的线程之一,因为它们此刻永久处于睡眠状态)解锁互斥锁,另一个线程将与线程 A 同时开始执行代码。可能不需要。
Maybe if we knew what you were thinking about using the mutex for we could give a better answer. Are you trying to unlock a mutex after a thread was canceled? Do you have code that can handle 2 threads at a time but not three and there is no mutex that lets 2 threads through at a time?
也许如果我们知道您在考虑使用互斥锁,我们可以给出更好的答案。您是否在取消线程后尝试解锁互斥锁?您是否有一次可以处理 2 个线程但不能处理三个线程的代码,并且没有一次让 2 个线程通过的互斥锁?