ClubRobotInsat / info-archive

Code informatique du Club Robot INSA Toulouse
MIT License
3 stars 1 forks source link

Cleaner closure of a multi-serial protocols communication. #3

Closed Terae closed 5 years ago

Terae commented 5 years ago

Is your feature request related to a problem? Please describe. On the file /src/robot/Communication/Protocol.hpp, function void recv_frame(const atomic_bool&, function<void(GlobalFrame)>: the ETHERNET communication creates N detached threads on all the lifetime of the communication.

At the end, when we want to stop it, the atomic_bool& is set to false and threads would exit, but only when they receive a frame on the associate socket (in order to exit the UDP blocking recv_from function).

In order to avoid sending an ampty frame to all UDP connections but only one, a for(thread t : pool) { pthread_cancel(t); } loop is used at the very end of the program; it isn't a dangerous feature because it does not compromise the communication itself or the core.

I would like a better ending function to stop all threads properly.

Describe the solution you'd like

  1. Create N receiving threads
  2. When a reception ends, a AbstractSerialProtocol<P>::ReceptionAborted exception is thrown so we need to catch it (maybe using the std::exception_ptr object
  3. Send an empty frame at all the other working threads to force them exit their receiving blocking function
gbip commented 5 years ago

Have you tried taking a look at promise and futures ? There is an example in the repo for the Can Interface application. See CanListener::mainLoop.