There is some multi-agent system that has a core, agent network, services, etc.
It is loaded and managed by a manager, let's call it
It also communicates with the user application, i.e. this manager owns the engineered kernel object, which the user application accesses through it. He is also responsible for deletion and preservation.
The question arose in the implementation of this manager. Implement it in a singleton way? There was a problem with calling the deleter i.e. in a multi-threaded user application, it will not be entirely clear when to call it, perhaps some thread will need access to the kernel, while another will already close (delete) it. On the other hand, if you allow the creation of multiple instances, then conceptually it looks strange. In fact, these instances will be identical, it is impossible to allow everyone to have a copy of the kernel -> the kernel is considered as a static object-member of the class, again it is not clear who will delete it. Here you have to do something with counting the number of connections (currently created instances).
Please advise how to implement such a service?
I adhere to this concept. The System_Manager
System_Manager is created in
main() , at the moment when it is convenient, it is possible on the stack. Then it "runs" the main code, for example,
app.exec() or something similar, which has a link to the
System_Manager . Then, when the user presses the [X] button or the command "quit" obtained in another way, it calls the "shutdown" of the
System_Manager to soft terminate all services or switch them to safe mode, note we exit
app.exec() , after which we wait for the completion of all threads that are associated with
System_Manager , and destroy it.