Ask Your Question

Revision history [back]

Well, do not use fork() in multi-threaded application unless you really know what you are doing. Please see Threads and fork(): think twice before mixing them.

Yes, roscpp is implicitly multi-threaded. There are at least two threads after invoking ros::init(), one is your main thread, while another belong to PollManager which essentially processes all incoming and outgoing messages by polling a list of sockets which were opened by publishers and subscribers.

As stated on the man page fork(2) and also mentioned on the link above:

The child process is created with a single thread—the one that called fork(). The entire virtual address space of the parent is replicated in the child, including the states of mutexes, condition variables, and other pthreads objects; the use of pthread_atfork(3) may be helpful for dealing with problems that this can cause.

No wonder data is not published. The PollManager is not running because its thread was not created in the forked process and thus it is not polling socket descriptors for incoming or outgoing data.

If you really want to mix this 'classical server approach' and ROS code, then I would suggest to try initializing roscpp after the fork(). In this case make sure you are not using any ROS functionality before that, e.g. do not call ros::ok() as it relies on the initialization state of the roscpp library.