两次解锁条件变量互斥X?

发布于 2025-02-03 02:55:38 字数 1056 浏览 2 评论 0原文

我正在查看以下片段:

#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 技术交流群。

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

发布评论

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

评论(2

吖咩 2025-02-10 02:55:38

该代码正在做的是有效的。

当线程调用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 from pthread_cond_wait.

无声静候 2025-02-10 02:55:38

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文