How to pass a c++ pointer between two nodelets?

asked 2020-01-08 11:16:31 -0600

annt0903 gravatar image

I have two nodelets A and B. The nodelet A contains a pointer to a C++ object (also allocated by A).

I want to pass the pointer to the nodelet B, then B can get/set the pointer's values normally.

The basic idea is that two nodelets share (modify) the same memory object.

I found a link that similar with my problem: but it just passing the copy of data not the pointer.

I would be happy to receive some feedback for this situation.

edit retag flag offensive close merge delete


You may already realise, but this completely couples the nodelets. That's typically not a good thing.

Perhaps you could describe what it is your're really trying to do and perhaps even more importantly: why.

gvdhoorn gravatar imagegvdhoorn ( 2020-01-08 12:59:17 -0600 )edit

I'm porting a window system to Ubuntu/ROS. On the window system, there is a main thread A and a sub-thread B (A creates B). Two threads communicate via mechanism of post message. Thread A hold a pointer object and pass it to the thread B. The pointer object is used for updating the system's status (I call it DataAccess)

While porting, I mimic the window's mechanism on Ubuntu/ROS. I use nodelet but not node because each nodelet is a thread as on window. I also use topic method (temporary) for communication between nodelets.

I don't know how to pass the pointer between the nodelets. At least I found two ideas: 1) Consider applying IPC or shared pointer. 2) Create a separate nodelet C for the pointer object, then the nodelets A, B can access the nodelet C via topic method.

annt0903 gravatar imageannt0903 ( 2020-01-08 20:21:00 -0600 )edit

What is the advantage of using nodelets in your ported system?

Are you using them just to use threads? If so: what is the reason to not just use a couple of regular threads in a single node?

gvdhoorn gravatar imagegvdhoorn ( 2020-01-09 01:48:09 -0600 )edit

One of reasons is use threads. But I also want to use ROS communication between threads (nodelets) too.

In addition, on my system, there are many sub-threads, thread B is just one of them, if use a couple of regular threads in a single node seems inappropriate?

Now I'm checking the ways shared memory between nodelets.

annt0903 gravatar imageannt0903 ( 2020-01-09 09:31:40 -0600 )edit

Without knowing exactly what you're trying to do it's hard to say. But it sounds as though you would be much better off using a single node with multiple threads as @gvdhoorn suggests. You can still create ROS msg objects and simply pass shared pointers to them between threads.

PeteBlackerThe3rd gravatar imagePeteBlackerThe3rd ( 2020-01-09 17:10:10 -0600 )edit