简体   繁体   English

是否可以在多个条件下使用 pthread 条件变量(pthread_mutex 和 pthread_cond_t)?

[英]Is it possible to use pthread conditon variable (pthread_mutex and pthread_cond_t )with multiple conditons?

I have a multithreaded program and eacch thread focuses on its own work.我有一个多线程程序,每个线程都专注于自己的工作。 When a thread finishes its work, it will drive the event to the main thread.当一个线程完成它的工作时,它会将事件驱动到主线程。

So I want to use the pthread condition variable to implement the idea (ie pthread_cond_t and pthread_mutex_t).所以我想用pthread条件变量来实现这个想法(即pthread_cond_t和pthread_mutex_t)。 The data structure will be like:数据结构将如下所示:

typedef struct peer_info {
    int ip; 
    int port;
    pthread_mutex_t peer_mutex;
    pthread_cond_t peer_cond;
    bool write_v;
    bool read_v;
    bool create_v;
    bool destroy_v;
} peer_info;

Suppose that:假设:
thread1 changes the write_v and signals main thread, thread1 改变 write_v 和信号主线程,
thread2 changes the read_v and signals the main thread, thread2 更改 read_v 并向主线程发出信号,
thread3 changes the create_v and signals the main thread, thread3 更改 create_v 并向主线程发出信号,
thread4 changes the destroy_v and signals the main thread. thread4 更改了 destroy_v 并向主线程发出信号。

Is it possible to use only one pthread_mutex and pthread_cond_t to implement the above scenario?是否可以只使用一个 pthread_mutex 和 pthread_cond_t 来实现上述场景? Will it cause a deadlock?会不会造成死锁?

Yes, you can safely wait on a "compound predicate," where one signal means "this state changed or that state changed or some other state changed or ..."是的,您可以放心地等待“复合谓词”,其中一个信号表示“此 state 已更改state 已更改其他一些 state 已更改...”

In your case, those states are the event_v flags you set.在您的情况下,这些状态是您设置的event_v标志。 Presuming your main thread is in a loop, and that the event_v flags are initialized to false , it'd look something like this:假设您的主线程处于循环中,并且event_v标志被初始化为false ,它看起来像这样:

// main loop:
pthread_mutex_lock(&pi.peer_mutex);
while (! (pi.write_v || pi.read_v || ... )) {
  pthread_cond_wait(&pi.peer_cond, &pi.peer_mutex);
}
if (pi.write_v) {
  pi.write_v = false; // clear this so we're not confused next time we wake up
  handle_write_event(&pi);
}
if (pi.read_v) {
  pi.read_v = false;
  handle_read_event(&pi);
}
if (...) ...          // handle other events

Note that this will handle events while the mutex is locked, which may or may not be OK for you.请注意,这将在互斥锁被锁定时处理事件,这对您来说可能合适,也可能不合适。

As an aside, I find all those boolean flags a bit cumbersome, and might use bit flags instead:顺便说一句,我发现所有这些 boolean 标志有点麻烦,可能会改用位标志:

#define PEER_EVENT_READ  1 << 0
#define PEER_EVENT_WRITE 1 << 1
#define PEER_EVENT_ ...

struct peer_info {
  short events_pending; // initialized to zero
  ...
};

So that I could concisely write:这样我就可以简洁地写:

while (! pi.events_pending) {
  pthread_cond_wait(&pi.peer_cond, &pi.peer_mutex);
}
if (pi.events_pending & PEER_EVENT_READ) { ... }
if ....
pi.events_pending = 0; // clear them all

But it's semantically the same thing, so up to you.但它在语义上是一样的,所以取决于你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM