简体   繁体   English

如何实现单个消息的消息传递?

[英]How to implement message passing for a single message?

I have implemented a job/task system. 我已经实现了工作/任务系统。 You can create a function<void, void> and place it on a thread local queue, before that I wrap the function in a Fiber. 您可以创建一个function<void, void>并将其放置在线程本地队列中,然后再将该函数包装在Fiber中。

Now that works perfectly fine at the moment but it would be quite useless if I can not return values. 现在,这可以正常工作,但是如果我不能返回值,那将毫无用处。 function<T, void>

That means I would need a way to do message passing. 这意味着我需要一种方法来传递消息。 I implemented it this way: 我是这样实现的:

I have a Cell<T> which can store a variable and has an atomic counter. 我有一个Cell<T> ,它可以存储变量并具有原子计数器。 I take a function<T, void> , wrap in in a closure. 我将一个function<T, void>封装在一个闭包中。 Once the function has been executed it will write into the cell and decrement the counter. 一旦函数执行完毕,它将写入cell并减少计数器。 Now when I create the closure I have to pointer to a cell, one is inside the closure and one is outside. 现在,当我创建closure我必须指向一个单元格,一个在闭包内部,一个在外部。

The closure is of type function<void, void> and I also wrap it in a fiber and put it in a thread local queue. 闭包的类型为function<void, void> ,我还将其包装在光纤中并将其放入线程本地队列中。

Now outside of the task system I can check that cell if the counter has decremented to 0. If it has I know the task must have finished, otherwise I have to wait. 现在,在任务系统之外,我可以检查该单元格,如果计数器已减小到0。如果有,我知道task一定已经完成,否则必须等待。 I only wait in the main thread outside of the task system. 我只在任务系统之外的主线程中等待。 Inside the task system I can just reschedule. 在任务系统内部,我可以重新安排时间。

I have not done any multihreaded programming before and this was the only way that I could imagine to implement "message passing". 我之前没有做过任何多用途编程,而这是我可以想象实现“消息传递”的唯一方法。

I am pretty sure that something like my Cell has already been invented, but I am not sure what the general name for it is. 我很确定像Cell这样的东西已经被发明了,但是我不确定它的通用名称是什么。

Is this a common way to implement message passing? 这是实现消息传递的常用方法吗? By that I mean "Wrapping a cell inside a closure and wait for completion". 我的意思是“在闭包中包装一个cell然后等待完成”。

What is the correct name of my Cell and are there any implementations of it available online? 我的Cell的正确名称是什么,在线上有任何可用的实现方式吗?

There is a way to pass one message, and it's called "future". 有一种传递消息的方法,称为“未来”。 A "future" is an object that will hold a value in (near) future (hence the name), and you can poll it (or wait) in order to get the value. “未来”是一个对象,它将在将来(因此)(因此而得名)中保存一个值,您可以对其进行轮询(或等待)以获取该值。 In C++11, it exists under the form of std::future . 在C ++ 11中,它以std::future的形式存在。 The emitter side of a future is a promise . 未来的发射方是一个promise

For your case: 对于您的情况:

  1. someone gives a std::function<T()> to be scheduled for execution 有人给std::function<T()>安排执行
  2. you wrap this function in a fiber. 您将此功能包装在光纤中。 While doing this, you should actually wrap it in a std::function<void()> first: 在执行此操作时,实际上应该先将其包装在std::function<void()>

     std::promise<T> promise; std::future<T> future = promise.get_future(); std::function<void()> wrapped = [f, p = std::move(promise)]() { p.set_value(f()); }; // do stuff with 'wrapped' return future; 
  3. With the returned future you can either wait for a value (blocking call) or poll it to know when the value is done. 使用返回的future,您可以等待一个值(阻塞调用),也可以对其进行轮询以了解何时完成该值。

Bonus: you can also create a std::future<void> , in which case you can still call get() or still poll it; 奖励:您还可以创建一个std::future<void> ,在这种情况下,您仍然可以调用get()或对其进行轮询; it will act a signal for the completion of the function. 它会为完成功能发出信号。

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

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