简体   繁体   English

如何在 .netstack 中使数据结构全局化,以便多个代理可以更新该数据结构中的信息?

[英]How to make data structure global in unetstack so that multiple agents can update information in that data structure?

Currently i have created 2D array containing neighbor information in one of the agents.目前我已经在其中一个代理中创建了包含邻居信息的二维数组。 I want this array to be global so that other agents can also access this array and update the information in the array.我希望这个数组是全局的,这样其他代理也可以访问这个数组并更新数组中的信息。 Please tell how to make a data structure or a variable global so others agents can also access that.请说明如何使数据结构或变量成为全局变量,以便其他代理也可以访问它。

In a distributed system, agents can, in theory, run on different containers and even different machines, and hence sharing a data structure or array isn't a good idea.在分布式系统中,理论上,代理可以在不同的容器甚至不同的机器上运行,因此共享数据结构或数组不是一个好主意。

Having said that, if you knew that the agents accessing the data structure were guaranteed to be running in the same container, you could share a reference to the data structure between agents and manipulate/access the data from different agents.话虽如此,如果您知道访问数据结构的代理保证在同一个容器中运行,您可以在代理之间共享对数据结构的引用并操作/访问来自不同代理的数据。 The reference may be shared during setup of the stack when the agents are constructed, by providing getters/setters where the reference could be obtained and passed to other agents.可以在构造代理时在堆栈设置期间共享引用,方法是提供可以获取引用并传递给其他代理的 getter/setter。 If you did this, you'd also need to ensure synchronization, as each agent runs in its own thread, and you could have data races.如果这样做,您还需要确保同步,因为每个代理都在自己的线程中运行,并且您可能会出现数据竞争。 Although this is easily doable, I would strongly advise against this approach.虽然这很容易做到,但我强烈建议不要使用这种方法。

A better approach would be to send messages between agents to get/update the shared information, or expose the shared information as agent parameters.更好的方法是在代理之间发送消息以获取/更新共享信息,或者将共享信息公开为代理参数。 For example, the NodeInfo agent does exactly this, by exposing commonly needed information (eg node address, node location, node orientation, etc) about a node as parameters that many other agents access and a few update.例如, NodeInfo代理正是这样做的,它通过公开关于节点的常用信息(例如节点地址、节点位置、节点方向等)作为许多其他代理访问的参数和一些更新。

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

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