How to share inter-dependent Struct/HashMap among threads using Arc, RwLock?

I am working on a project which includes

  • a Graph,
  • a list of Node structs (not related to the Graph) and
  • a mapping from the Graph's internal node to the Node struct.

I want to run one thread for each of the N nodes inside the Graph, where

  • struct Node encloses the Finite State Machine code required by each of the threads.

For this, after generating the Graph from user input, I create

  • a new struct Node for internal node in the Graph and
  • a Mapping (HashMap) from the Graph's internal node to the Node struct.

For the needed functionality, each thread needs access to

  • mutable Graph,
  • mutable Node struct (corresponding to the current thread) and
  • immutable access to all the other Node structs.
    and each thread needs a way to communicate with other thread.
    (I'm trying to use "Arc<RwLock<>>" to pass the Node struct with three of its fields being the Graph, the Mapping and the Channel's two halves.)

Each thread after spawning, needs to call a Node::initialize() method (which finds the least weight edge-node, sends a message to the node and changes Node's (self) state).

`Node { 
index: NodeIndex,
graph: Arc<RwLock<Graph>>,
mapping: Arc<RwLock<HashMap<NodeIndex, RwLock<Node>>>>

Is this the right approach? Any suggestions? Help appreciated!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.