两次解锁条件变量互斥X?
我正在查看以下片段:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cvar;
char buf[25];
void*thread_f(void *);
int main(){
pthread_t thr;
pthread_cond_init(&cvar,NULL);
pthread_mutex_lock(&mtx);
pthread_create(&thr,NULL,thread_f,NULL);
pthread_cond_wait(&cvar,&mtx);
pthread_mutex_unlock(&mtx);
...join and destroy thread...
}
和thread_f
:
void* thread_f(void* argp){
pthread_mutex_lock(&mtx);
strcpy(buf,"test");
pthread_cond_signal(&cvar);
pthread_mutex_unlock(&mtx); //why?
pthread_exit(NULL);
}
我对上述代码的理解是,main
抓住锁定,创建线程并运行阻止的thread_f。然后,Main向条件变量上的等待CVAR
,并解锁MUTEX。然后,thread_f
解开,strcpys和Signals cvar
唤醒。然后,Main和thread_f解锁二线。
这是实际的行为,还是我在某处缺少某些东西?如果这是实际行为,不是解锁已经解锁的Mutex ub,因此,最终是否将其中一个静音解锁去除?
谢谢。
I'm looking at the following snippets:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cvar;
char buf[25];
void*thread_f(void *);
int main(){
pthread_t thr;
pthread_cond_init(&cvar,NULL);
pthread_mutex_lock(&mtx);
pthread_create(&thr,NULL,thread_f,NULL);
pthread_cond_wait(&cvar,&mtx);
pthread_mutex_unlock(&mtx);
...join and destroy thread...
}
and thread_f
:
void* thread_f(void* argp){
pthread_mutex_lock(&mtx);
strcpy(buf,"test");
pthread_cond_signal(&cvar);
pthread_mutex_unlock(&mtx); //why?
pthread_exit(NULL);
}
My understanding of the above code is, that main
grabs the lock, creates the thread and runs thread_f which blocks. Then, main signals the wait on the condition variable cvar
and unlocks the mutex. Then, thread_f
unblocks, strcpys and signals cvar
to wake up. Then, both main and thread_f unlock the mutex.
Is this the actual behaviour, or am I missing something somewhere? If it's the actual behaviour, isn't unlocking the already unlocked mutex UB, therefore should one of he mutex unlocks in the end be removed?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该代码正在做的是有效的。
当线程调用
pthread_cond_signal
时,它不会立即导致主线程抓住Mutex,而是唤醒主线程,以便可以尝试锁定它。然后,一旦线程释放了互斥,主线程就可以锁定并从pthread_cond_wait
返回。What this code is doing is valid.
When the thread calls
pthread_cond_signal
it doesn't immediately cause the main thread to grab the mutex, but wakes up the main thread so it can attempt to lock it. Then once the thread releases the mutex, the main thread is able to lock it and return frompthread_cond_wait
.pthread_cond_wait()
在主线程中解锁互斥X并等待条件发出信号 - 并在返回之前重新锁定互斥X。子螺纹能够锁定静音,操纵缓冲区,发出信号,然后解锁静音,让主螺纹重新锁定锁。
因此,该代码的行为良好 - 给予或需要担心'smerious wakeups'在更一般的情况下多个子线。我认为您无法在这里被虚假唤醒。
The
pthread_cond_wait()
in the main thread unlocks the mutex and waits for the condition to be signalled — and relocks the mutex before returning.The child thread is able to lock the mutex, manipulate the buffer, signal the condition and then unlock the mutex, letting the main thread reacquire the lock.
Consequently, the code is well-behaved — give or take the need to worry about 'spurious wakeups' in more general situations with multiple child threads. I don't think you can get a spurious wakeup here.