简体   繁体   English

Poco Poco :: NotificationQueue无法按预期工作

[英]Poco Poco::NotificationQueue not working as expected

I'm starting using Poco::NotificationQueue . 我开始使用Poco::NotificationQueue I have noticed that strange behavior, if more than one worker-threads are waiting on the same NotificationQueue using waitDequeueNotification() , only one of the tread receives a notification sent from a master-thread. 我注意到一种奇怪的行为,如果使用waitDequeueNotification()在同一个NotificationQueue上等待多个工作线程,则只有一个脚踏板会收到从主线程发送的通知。 This is obvious if you run the NotificationQueue.cpp example provided with Poco (in this case done from Visual Studio, but the result is the same when tested on Linux). 如果您运行Poco附带的NotificationQueue.cpp示例(在这种情况下是从Visual Studio完成的,但是在Linux上进行测试的结果是相同的),则这是显而易见的。

The output from it is : 它的输出是:

 Worker 1 got work notification 0 Worker 3 got work notification 2 Worker 2 got work notification 1 Worker 1 got work notification 3 Worker 3 got work notification 4 Worker 2 got work notification 5 ... 

Expected result would be: 预期结果将是:

 Worker 1 got work notification 0 Worker 3 got work notification 0 Worker 2 got work notification 0 Worker 1 got work notification 1 Worker 3 got work notification 1 Worker 2 got work notification 1 ..... 

Is this a bug or what? 这是错误还是什么? If so is there any workaround? 如果是这样,有什么解决方法吗?

This is not a bug, but your expectations are wrong. 这不是错误,但是您的期望是错误的。

The NotificationQueue is designed to notify the Worker threads about jobs to do. NotificationQueue旨在向Worker线程通知要执行的作业。 each Notification represents a job to complete. 每个Notification代表要完成的工作。 If Worker 1 is already working on the task, why should the next free Worker start working on the same task? 如果Worker 1已经在执行任务,为什么下一个空闲的Worker应该开始在同一任务上工作? The whole use of multithreading is parallelization of work. 多线程的整个用途是工作并行化。 So multiple jobs can be processed ad the same time. 因此,可以同时处理多个作业。

If you want each Thread to receive the same Notification , you should not use a NotificationQueue . 如果你想每个线程收到相同的Notification ,你就不应该使用NotificationQueue Instead you should use a NotificationCenter with Observers . 相反,您应该将NotificationCenterObservers

See these slides for more information: NotificationsEvents.pdf 有关更多信息,请参见以下幻灯片: NotificationsEvents.pdf

Actually it's design intend. 实际上是设计意图。 You are obtaining new notification invoking waitDequeueNotification() or dequeueNotification() . 您正在获取新的通知,其中包括调用waitDequeueNotification()dequeueNotification()通知。 Both functions get notification and do remove it from the appropriate queue. 这两个函数都获得通知,并确实将其从适当的队列中删除。

If you wish to notify every working thread, you need to send notification for each thread separately (every working thread should have own queue). 如果希望通知每个工作线程,则需要分别发送每个线程的通知(每个工作线程都应具有自己的队列)。

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

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