简体   繁体   English

多台计算机的C ++多线程

[英]C++ Multi-threading with multiple machines

Well my problem is the following. 好吧,我的问题如下。 I have a piece of code that runs on several virtual machines, and each virtual machine has N interfaces(a thread per each). 我有一段代码可以在多个虚拟机上运行,​​并且每个虚拟机都有N个接口(每个接口一个线程)。 The problem itself is receiving a message on one interface and redirect it through another interface in the fastest possible manner. 问题本身就是在一个接口上接收到一条消息,然后以最快的方式将其重定向到另一个接口。

What I'm doing is, when I receive a message on one interface(Unicast), calculate which interface I want to redirect it through, save all the information about the message(Datagram, and all the extra info I want) with a function I made. 我正在做的是,当我在一个接口(单播)上收到消息时,计算要重定向的接口,并使用函数保存有关消息的所有信息(数据报和我想要的所有其他信息)我做了。 Then on the next iteration, the program checks if there are new messages to redirect and if it is the correct interface reading it. 然后,在下一次迭代中,程序将检查是否有新消息要重定向,以及是否是读取它的正确接口。 And so on... But this makes the program exchange information very slowly... 依此类推...但这使程序交换信息的速度非常慢...

Is there any mechanism that can speed things up? 有什么机制可以加快速度吗?

Somebody has already invented this particular wheel - it's called MPI 有人已经发明了这种特殊的轮子-叫做MPI

Take a look at either openMPI or MPICH 看看openMPIMPICH

Why don't you use queuing? 为什么不使用排队? As the messages come in, put them on a queue and notify each processing module to pick them up from the queue. 当消息进入时,将它们放在队列中,并通知每个处理模块从队列中提取它们。 For example: 例如:

  • MSG comes in 味精进来
  • Module 1 puts it on queue 第1单元将其放入队列
  • Module 2,3 get notified 模块2,3收到通知
  • Module 2 picks it up from queue and saved it in the database 模块2从队列中提取它并将其保存在数据库中
  • In parallel, Module 3 picks it up from queue and processes it 并行地,模块3从队列中提取并处理它

The key is "in parallel". 关键是“并行”。 Since these modules are different threads, while Module 2 is saving to the db, Module 3 can massage your message. 由于这些模块是不同的线程,因此当模块2保存到数据库时,模块3可以处理您的消息。

You could use JMS or MQ or make your own queue. 您可以使用JMS或MQ或创建自己的队列。

It sounds like you're trying to do parallel computing across multiple "machines" (even if virtual). 听起来您正在尝试跨多个“机器”(即使是虚拟机)进行并行计算。 You may want to look at existing protocols, such as MPI - Message Passing Interface to handle this domain, as they have quite a few features that help in this type of scenario 您可能希望查看现有协议,例如MPI-消息传递接口来处理此域,因为它们具有许多有助于此类情况的功能

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

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