C ++ PPL make_choice [英] C++ PPL make_choice

查看:63
本文介绍了C ++ PPL make_choice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题可通过MSDN文档中的示例重现


https://msdn.microsoft.com/en-us/library/dd504833.aspx


参考示例位于网页的中间位置,"选择类"我的问题是unbounded_buffer


我修改了single_assignment和overwrite_buffer,最后通过将35和37更改为更高的数字,将
数字用于unbounded_buffer更低的数字,
所以它完成得更快。


Switch(receive(select_one)//似乎消耗了这条消息


接收(selection_one)将正确识别案例2,即
与unbounded_buffer一起使用,但它会消耗unbounded_buffer中的消息。 当达到$ b $时b 案例2:并执行接收( fib的一半),功能块因为没有消息到
receive 因为前一个接收函数删除了它。 
案例0, 1 都可以工作,因为您可以根据需要多次执行接收功能,而不会消耗消息。


然而,在案例2中,它无限期地阻止,我唯一的解决方案是发出
一个双发​​送,但似乎该示例旨在能够在识别选择了哪个消息源后接收消息。


我的用例比示例复杂一点,我有几个相同的代理使用相同的无界缓冲区作为源,工作正常,直到我需要
make 选择(在管道中),如果我发出一个双重发送,如果2个代理消耗每个输入消息,它可能会死锁,然后阻塞2
nd

接收消息。  因此,消息可能会丢失,因为在选择器接收中消耗了


< p style ="margin:0px 0px 10.66px"> 我的问题是,我如何收到
消息在没有
a double
发​​送的无界缓冲区中。 是否可能或者这是PPL库中的缺陷



感谢您提供任何帮助。


< style>< / style>

解决方案

我认为你误解了它是如何工作的。  "接收"总是消费消息。 一旦你消费了消息,它就消失了。 在该示例中,"select(receive(select_one))"从select_one
对象中提取(并使用)一条消息,该消息是一个整数,表示首先完成了哪个选项。 然后,各个case语句根据选择结果从该单个对象中提取(并使用)消息。


因此,receive(select_one)不能使用来自unbounded_buffer的消息。  ;该消息必须使用receive(half_of_fib42)获取。


它就像带有一卷票的售票机。 一旦拉票,它就消失了。 没有其他人可以拉它。


如果你有多个读者想要拉一条信息,那么其中一个会赢,其中一个会空出来。 这就是它的工作方式。 如果一条消息需要触发多个操作,那么您需要以这种方式构建应用程序
。 让一个线程监听结果,然后使用其他一些机制来触发多个动作。


my problem is reproducible via the example found on MSDN document

https://msdn.microsoft.com/en-us/library/dd504833.aspx

The reference example is found halfway down the webpage, "choice Class" my problem is with unbounded_buffer

I have modified both single_assignment, and overwrite_buffer, to end execution last by changing 35 and 37 to higher numbers, and the number used for the unbounded_buffer to a lower number, so it completes faster.

Switch(receive(select_one) // seems to consume the message

The receive(selection_one) will correctly identify case 2, that is used with the unbounded_buffer, but it will consume the message in the unbounded_buffer.  when reaching Case 2:, and executing receive (half of fib), the function blocks because there are no messages to receive because the previous receive function removed it.  Case 0, and 1 both work because you can execute a receive function as many times as you want without consuming the message.

However, in case 2, it blocks indefinitely, my only solution is to issue a double send, but it seems the example is intended to be able to receive the message after the identifying which message source was selected.

My use case is a bit more complicated than the example, I have several identical agents using the same unbounded buffer as a source, that works fine, until I needed to make a choice(in the pipeline), if I issue a double send, it may deadlock if 2 agents consume each of the input messages, and then block on the 2nd receive message.  So, the message may get lost because it was consumed in the selector receive

My question is, how do I, receive the message in the unbounded buffer without a double send.  Is it possible or is this a flaw in the PPL library

Thank you for any help you can provide

<style></style>

解决方案

I think you misunderstand how this works.  "receive" always consumes the message.  Once you consume the message, it's gone.  In the example, the "select(receive(select_one))" pulls (and consumes) a message from the select_one object, which is an integer that says which of its choices completed first.  The individual case statements then pull (and consume) a message from that individual object, based on the choice result.

So, receive(select_one) cannot possibly consume a message from the unbounded_buffer.  That message would have to be fetch using receive(half_of_fib42).

It's exactly like a ticket machine with a roll of tickets.  Once a ticket is pulled, it's gone.  No one else can pull it.

If you have multiple readers trying to pull a single message, then one of them will win, and one of them will come up empty.  That's just the way it works.  If one message needs to trigger multiple actions, then you need to architect your application that way.  Have one thread listening for the result, then use some other mechanism to trigger the multiple actions.


这篇关于C ++ PPL make_choice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆