I have a dll which exports the interface
class Qwe{
virtual void a() = 0;
virtual void b() = 0;
};
extern "C" Qwe* createQwe();
I load it in my program and create multiple Qwe objects. The question is whether accessing those objects from different threads is safe? Shall I open the new library instance for each object, or one is enough?
No static data members are used, and library functions are not thread-safe by themselves.
To define the question better… exported class may look like this
class QweImpl : public Qwe{
public:
virtual void a() {
std::fill(data.begin(), data.end(), 1.0)};
private:
std::vector<double> data; };
is QweImpl::a() safe here? In the sense that two different QweImpl objects can simultaneously call their a() functions?
In windows you can only load a library once. Further attempts to load a library only increments a reference counter. So therefore ‘loading a library for each object’ is a non-starter.
If your Qwe class contains data members or fields, which I can’t tell if it does or not, than you could protect access to shared memory using critical sections (or some other mechanism) inside of Qwe.