等待 C++ 中的通知

发布于 2024-12-09 14:30:43 字数 506 浏览 0 评论 0原文

与 C++ 并不严格相关,我正在寻找更多关于如何解决这个问题的设计模式或建议。

假设我有,

class A
{
public:
   void process();
   void wait();
}

我将首先调用 process(),它(duuh)会进行一些处理,然后调用 wait()wait() 函数应该等待通知然后退出。我已经在单独的线程上有了通知的逻辑,但我不确定最好的方法是什么。

我想到的是:

void A::wait()
{
   while ( _notificationOccured == false )
   {
   }
}

其中 _notificationOccured 可以是 A 的 bool 成员,它将被通知更改。但是,我再次不确定这是最好的方法。有什么建议吗?

Not strictly related to C++, I am looking for more of a design patter or suggestion on how to approach this.

Say I have

class A
{
public:
   void process();
   void wait();
}

I will first call process(), which (duuh) does some processing and will then call wait(). The wait() function is supposed to wait for a notification and then exit. I already have the logic for the notification on a separate thread, but I'm not really sure what the best approach for this is.

What I thought of is:

void A::wait()
{
   while ( _notificationOccured == false )
   {
   }
}

where _notificationOccured can be a bool member of A that will be changed by the notification. But, again, I'm not sure that this is the best approach. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

枉心 2024-12-16 14:30:43

变量池的性能很差,因为池线程几乎占用了所有的 CPU 时间。您需要使用事件或消息 - 这些东西是特定于平台的。您可以为此使用一些可移植库,例如 Boost。

Pooling for a variable gives terrible performance, because pooling thread takes almost all CPU time. You need to use events or messages - this stuff is platform-specific. You can use some portable library for this, for example, Boost.

若水般的淡然安静女子 2024-12-16 14:30:43

您所做的称为<​​a href="http://en.wikipedia.org/wiki/Busy_waiting" rel="nofollow">忙等待。

有多种技术可以更好地做到这一点,最简单的是使用带有 ncondition 通知的普通互斥体(win32/pthreads/boost)。

What you do is called busy waiting.

The are various techniques to do this better, the simplest would be to use a plain mutex with ncondition notification (win32/pthreads/boost).

太阳哥哥 2024-12-16 14:30:43

您当前的方法引入了电源环路,这会降低您正在运行的系统的性能。您应该引入较短的睡眠时间(10 毫秒就足够了)来防止这种情况发生。更好的是,使用库,例如​​ Boost (正如@Nim 建议的那样)。

顺便说一句,像你这样进行民意调查并不全是坏事。事实上,这就是所谓的自旋锁所做的。这个想法是,如果预期等待时间很短,那么短时间的轮询比锁定更有效。

Your current approach introduces a power-loop, which will kill the performance of the system you are running on. You should introduce a short sleep-time (10ms will suffice) to prevent that from happening. Better yet, use a library, like Boost (as @Nim suggested).

Btw, polling like you do is not all bad. In fact, that is what so-called spin-locks do. The idea is that a short time of polling is more efficient than locking if the expected wait-time is short.

扭转时空 2024-12-16 14:30:43

两个选项:

  1. 信号量
  2. 条件

两者都是特定于操作系统的,boost 支持后者。还有其他方法(例如原子操作,但如何公开这些方法是编译器特定的)。恕我直言,我会使用上述之一。

Two options:

  1. Semaphores
  2. Conditions

Both are OS specific, boost has support for latter. There are other ways (such as atomic operations, but how these are exposed is compiler specific). IMHO, I would use one of the above.

你不是我要的菜∠ 2024-12-16 14:30:43

我只在 Windows 上知道这一点,所以我不知道这是否可以轻松转换到其他平台。

在伪代码中:

Timer myTimer(1, MYEVENT); // elapses every second
SetTimer( myTimer );       // register timer with event loop

while( running )
{
  if( GetEvent() == MYEVENT )
  {  

  }
}

在 Windows 中 GetEvent() 被称为 WaitForSingleObject(...)

I only know this from Windows, so I don't know if this translates easily to other plattforms.

In pseudo code:

Timer myTimer(1, MYEVENT); // elapses every second
SetTimer( myTimer );       // register timer with event loop

while( running )
{
  if( GetEvent() == MYEVENT )
  {  

  }
}

In Windows GetEvent() is called WaitForSingleObject(...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文