Encapsulating an async task in an object


I would like to have an object encapsulating a task and being able to simple start, stop and join the inner task from external methods. In my example, main is starting and joining the task and another task handling a signal handler is sending the stop command.
I use options to store a tx channel and the join handle of the inner task. My problem is that by doing this I need mutable refs and thus Arcs and mutexes to enable calling it from other tasks. By doing so I ended up into deadlock as the main locks the mutex englobing the object to join the inner handle and the signal handle cannot lock it to call the stop method. I would prefer not to expose the tx channel and the join handle to outside of the object.

I ended up with the following code Rust Playground. I commented out the parts preventing the compilation as I did not include the arc and mutexes on this version.

Any hints about how I shall solve this?

Ended up to use two mutexes inside the object itself instead to protect the object, letting me to use inner-mutability.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.