简体   繁体   English

将WIN32应用程序移植到Linux-事件在Linux中如何工作?

[英]Porting WIN32 Application to Linux - How does event work in Linux?

I have a WIN32 application and I'm porting it to LINUX GNU. 我有一个WIN32应用程序,并将其移植到LINUX GNU。 I have event base C code in WIN32 app. 我在WIN32应用程序中有基于事件的C代码。 Now i have tried few methods to implement the same in GNU linux but somehow i'm getting feeling that this code will not work properly. 现在,我尝试了几种在GNU linux中实现相同方法的方法,但是以某种方式,我感到这段代码无法正常工作。

First of all, I created a structure to implement event. 首先,我创建了一个用于实现事件的结构。

typedef struct _Event
   {
      int m_bool;
      pthread_mutex_t m_mutex;
      pthread_cond_t m_condition;

   }MyEvent, * Event_handle;

To implement 'SetEvent' , 'ResetEvent', 'WaitForSingleObject', i have implemented following code. 为了实现“ SetEvent”,“ ResetEvent”,“ WaitForSingleObject”,我已经实现了以下代码。

   MyEvent CreateEvent( void )
   {
       MyEvent e1;
       e1.m_bool = 1;
       return e1;
   }

   void SetEvent( MyEvent evt )
   {
      evt.m_bool = 1;
      pthread_cond_broadcast(&evt.m_condition);
   }

   void ResetEvent( MyEvent evt )
   {
      evt.m_bool = 0;
      pthread_cond_broadcast(&evt.m_condition);
   }

   int WaitForSingleObject( MyEvent evt, unsigned timeout )
   {
         pthread_cond_wait(&(evt.m_condition),&(evt.m_mutex));
         return SUCCESS;
   }

Now m confused about the usage of m_mutex and m_bool. 现在,m对m_mutex和m_bool的用法感到困惑。 What i'm looking for is a sample code or demo. 我正在寻找的是示例代码或演示。

I have tried all my keys to the lock but seems the door is still locked. 我已经尝试过所有的锁钥匙,但似乎门仍然锁着。 Any help will do. 任何帮助都可以。 Thanks ! 谢谢 !

It seems, I've found what you want. 看来,我找到了你想要的。 I'm not sure though. 我不确定。 My first thought was you are not understanding why the mutex is here. 我的第一个想法是您不了解为什么互斥锁在这里。 I'll try explain it first. 我会先解释一下。

On Windows©, when you call WaitForSingleObject() , it blocks the execution until you send a signal (or set an event in the signalled state, SetEvent() , in Windows terms). 在Windows©上,当您调用WaitForSingleObject() ,它将阻止执行,直到您发送信号为止(或将事件设置为信号状态SetEvent() ,在Windows中是这样)。 Basically, it's done by setting an implicit mutex inside the event object, locking it in the WaitForSingleObject() function and waiting for a signal. 基本上,这是通过在事件对象内设置隐式互斥锁并将其锁定在WaitForSingleObject()函数中并等待信号来完成的。

In the POSIX world, you need to set up the mutex explicitly. 在POSIX世界中,您需要显式设置互斥锁。 One important notice: there's no ResetEvent -link behaviour—you don't need to reset your conditional variable; 一个重要的注意事项:没有ResetEvent -link行为-您不需要重置条件变量; however, you need to unlock the mutex. 但是,您需要解锁互斥锁。 Probably, the sole purpose of the ResetEvent() function is to unlock that implicit mutex. 可能, ResetEvent()函数的唯一目的是解锁ResetEvent()式互斥量。

void SetEvent(MyEvent evt)
{
    pthread_cond_signal(&evt.m_condition);
}

void ResetEvent(MyEvent evt)
{
    pthread_mutex_unlock(&evt.m_mutex);
}

int WaitForSingleObject(MyEvent evt, unsigned)
{
    pthread_mutex_lock(&evt.m_mutex);
    pthread_cond_wait(&evt.m_condition, &evt.m_mutex);
}

In you case, the pthread_cond_signal() is more appropriate. 在这种情况下, pthread_cond_signal()更合适。 Also note that you need to use the pthread_cond_timedwait() function to mimic the WaitForSingleObject() function. 还要注意,您需要使用pthread_cond_timedwait()函数来模仿WaitForSingleObject()函数。

Further reading: 进一步阅读:

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

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