c - 如何更改 pthread_cont_wait 中的互斥体

标签 c pthreads posix

我有一个问题。如果q->mutex必须是sem_t

,我如何使用pthread_cond_wait
while(q->thread_lock[pom] < 0)
            pthread_cond_wait(&(q->cond), &(q->mutex));
        q->pthread_unlock[pom]--;

现在 q->mutexpthread_mutex_t,但这必须是 sem_t

结构体

typedef struct{
        int *thread_lock;
        int *thread_unlock
        pthread_mutex_t mutex;
        pthread_cond_t cond;

}q

我想这样做:

typedef struct{
        int *thread_lock;
        int *thread_unlock
        sem_t mutex;
        pthread_cond_t cond;

}q

我有这个问题: https://i.ibb.co/fdHwMjx/blod.jpg

最佳答案

您不能将 sem_tpthread_cond_wait() 一起使用,它需要 pthread_mutex_t

从根本上讲,互斥体/条件变量范例与信号量的范例不同 - 像这样将它们组合起来没有任何意义。我建议发布一个新问题来描述您要解决的根本问题。

关于c - 如何更改 pthread_cont_wait 中的互斥体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56621878/

相关文章:

c - C 中的新节点,如何修复结构?

c++ - 是否设置了 set-user-id 位?

c - 从 C 中的 fscanf 字符串中删除特殊字符

c - fgets 在没有空间的情况下阅读

php - 在 Debian 中使用 pthreads 编译 PHP

C pthreads错误

perl - POSIX pselect 在 Perl 中可用吗?

c - C中指针值变化的可能原因?

bash - 仅使用 Bash 模式匹配从字符串开头删除模式

c - 文件名上的 Posix I/O 操作顺序一致吗?