- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 Linux 中使用 pthread 实现手动重置事件,这类似于 Windows 中的 WaitForSingleEvent。我找到了这篇文章
pthread-like windows manual-reset event
然后跟着它,但是有一点让我感到困惑:
void mrevent_wait(struct mrevent *ev) {
pthread_mutex_lock(&ev->mutex);
while (!ev->triggered)
pthread_cond_wait(&ev->cond, &ev->mutex);
pthread_mutex_unlock(&ev->mutex);
}
我害怕的是当 pthread_cond_wait 释放互斥锁时,然后 pthread_mutex_unlock 可能会出现未定义的行为(这种事情会让我发疯,他们怎么不处理它:-D)
谢谢。
最佳答案
Upon successful return, the mutex has been locked and is owned by the calling thread.
这意味着当返回时,pthread_cond_wait
自动 锁定关联的互斥体。
工作流程是这样的:
pthread_cond_wait
原子地阻塞和解锁互斥锁(这样其他线程可能会到达这里)pthread_cond_wait
自动返回并锁定互斥体I don't think pthread_cond_wait blocks and unlocks
那是因为你没有阅读我提供的链接。
These functions atomically release mutex and cause the calling thread to block on the condition variable cond;
关于c++ - pthread_cond_wait 和 pthread_mutex_unlock 是否冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6532767/
我有以下源代码(改编 self 的原始代码): #include "stdafx.h" #include #include #include "pthread.h" #define MAX_ENT
pthread_mutex_unlock 在互斥锁上被调用,另一个具有更高实时优先级的线程正在等待那个互斥锁。是在这样的系统调用期间完成上下文切换,还是仅在量子结束后线程才会被抢占?如果在这种情况下无
假设一个线程成功调用pthread_mutex_lock,在同一线程中调用pthread_mutex_unlock是否仍然可能失败?如果是这样,除了中止线程之外,您实际上还能做些什么吗? if(pth
我正在开发一个也能处理错误的线程安全类。我想知道如何处理来自函数 pthread_mutex_unlock() 的可能错误。如果我抛出互斥体仍然被锁定?我应该尝试再次解锁还是销毁类对象? int So
我在 C 中有以下代码: pthread_cleanup_push(pthread_mutex_unlock, &mutex); 但是当我编译它时,我收到以下警告: warning: initiali
假设我锁定了一个名为 wfg 的互斥锁 pthread_mutex_lock(&wfg); //and then I return from the function return 0; 互斥体会保持
来自多处理器编程的艺术, 1 #include 2 #define QSIZE 16 3 typedef struct { 4 int buf[QSIZE]; 5 long head
一旦线程被取消,就需要解锁互斥体,以避免死锁。所以我设计了以下方法: // file_a.c pthread_attr_t attr; ... rc2 = pthread_attr_init(&att
我想知道如果不是多线程上下文,pthread_mutex_lock和pthread_mutex_unlock会造成多少开销,所以我写了一个demo: #include #include #incl
我在 Linux 中使用 pthread 实现手动重置事件,这类似于 Windows 中的 WaitForSingleEvent。我找到了这篇文章 pthread-like windows manua
我用 pthread 编写了一个多线程程序,使用生产者-消费者模型。 当我使用英特尔 VTune 分析器来分析我的程序时,我发现生产者和消费者在 pthread_mutex_unlock 上花费了大量
我在 SAP 代理日志中收到以下错误, (5538F3C3.0122-3154:pthread_mutex_unlock.c,44,"pthread_mutex_unlock") errno EAGA
我已成功使用 makecontext/swapcontext 移动堆栈。但是,当我尝试将它与 pthread_mutex_lock 或 pthread_mutex_unlock 一起使用时,我总是
这个问题在这里已经有了答案: Is function call an effective memory barrier for modern platforms? (4 个答案) 关闭 4 年前。
我有一段代码需要快速运行,现在我正在使用 pthread_mutex_lock/pthread_mutex_unlock 来同步线程,但我发现它对性能有一定的影响。我想知道,如果有人对此进行基准测试,
我是一名优秀的程序员,十分优秀!