jeudi 29 décembre 2016

In C++, can two other different shared objects access a Singleton from a third shared object?

I have an application in C++, which loads most of its code from two or more plugins (each with at least one thread). These plugins are '.so' files, it is, share objects. It uses the following code to open the plugins:

pluginHandle = dlopen(fileName, RTLD_NOW|RTLD_GLOBAL);
init_t* init = (init_t*) dlsym(pluginHandle, "init") // Create should return an instance of the class of the plugin
plugin = init();

I arrived to the point to which I need two of those plugins to start adding data to a common Queue. As the application does not allow for communication between both plugins without changing the code in the application itself (a point we are trying to avoid), I think I found a way to solve that:

A dynamic library (shared object), which includes a singleton class with a thread-safe Queue. I would then recompile and link both plugins against the lybrary, and use getInstance() to get the singleton and start adding tasks to the queue.

Is that a safe implementation? Will the singleton Queue work?

Aucun commentaire:

Enregistrer un commentaire