简体   繁体   English

pthread_cond_t的EventWaitHandle行为

[英]EventWaitHandle behavior for pthread_cond_t

I've recently seen the light of EventWaitHandle's powerful behavior in C# and decided to move some functionality in a sister application to do the same. 我最近看到了EventWaitHandle在C#中的强大行为,并决定在姐妹应用程序中移动一些功能来做同样的事情。 The only problem is that the sister app is written in C. 唯一的问题是姐妹应用程序是用C语言编写的。

No big deal, I'm using pthreads, which have a pthread_cond_t datatype that allows for signalling. 没什么大不了的,我正在使用pthreads,它有一个允许信令的pthread_cond_t数据类型。 My only question is, is it possible for a cond to be 'signalled' before something is waiting on it? 我唯一的问题是,在某件事情等待它之前,是否有可能“发出信号”?

Right now my testing says no. 现在我的测试说没有。 That is, if ThreadA fires a signal before ThreadB is waiting, ThreadB will wait indefinately. 也就是说,如果ThreadA在ThreadB等待之前触发信号,则ThreadB将无限期地等待。 Is there another pthread type that I can use that behaves closer to the functionality of the EventWaitHandle in C#? 是否有另一种我可以使用的pthread类型,它更接近C#中EventWaitHandle的功能? An object is signalled, meaning that the first thread to wait on it, will pass immediately, and set it to unsignalled. 一个对象被发出信号,意味着要在其上等待的第一个线程将立即通过,并将其设置为unsignalled。

Wrapping the pthread_cond into another data structure wouldn't be too hard to achieve this. 将pthread_cond包装到另一个数据结构中并不会太难实现。 But again, is this functionality already available in the pthread library? 但同样,这个功能是否已在pthread库中提供?

If you're using condition variables correctly, this won't matter. 如果您正确使用条件变量,这无关紧要。

The basic flow of your code should be (in pseudocode): 代码的基本流程应该是(伪代码):

lock(lockobj);
while (!signalled) {
    wait(condvar);
}
signalled = false;
unlock(lockobj);

on the waiting side, and: 在等待方面,并且:

lock(lockobj);
signalled = true;
notify(condvar);
unlock(lockobj);

on the signalling side. 在信号方面。 (Of course, the lock object and condition variable used have to be the same on both sides.) Hope this helps! (当然,两侧使用的锁定对象和条件变量必须相同。)希望这有帮助!

Alternative answer (also in pseudocode) if you want multiple signallings (ie, if signalled twice, then two threads can wait before the state is unsignalled again). 如果你想要多个信号 (例如,如果发信号通知两次,那么两个线程可以在状态再次取消信号之前等待),可选择的答案(也是伪代码)。

Waiting side: 等待方:

lock(lockobj);
while (signalled != 0) {
    wait(condvar);
}
--signalled;
unlock(lockobj);

Signalling side: 信号方面:

lock(lockobj);
++signalled;
notify(condvar);
unlock(lockobj);

I ended up just wrapping a condition type in a new structure and created some simple functions to behave much like the EventWaitHandle from C#. 我最终只是将一个条件类型包装在一个新结构中并创建了一些简单的函数,其行为与C#中的EventWaitHandle非常相似。 I needed two mutexes to acheive proper serialized access. 我需要两个互斥锁来实现正确的序列化访问。

The the cond_mutex is used for waiting on the conditional variable, while the data_mutex is used when setting the state from signaled to not signaled. cond_mutex用于等待条件变量,而data_mutex用于将状态从信号设置为未信号。

The reset mode is the same from C#. 复位模式与C#相同。 AUTO or MANUAL. 自动或手动。 This allows for the event_wait_t to reset itself automatically after waiting. 这允许event_wait_t在等待后自动重置。 Or letting the programmer manually do it with a call to event_wait_reset(event_wait_t *ewh); 或者让程序员通过调用event_wait_reset手动执行它(event_wait_t * ewh);

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

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