make singleton thread safe

This commit is contained in:
2023-10-30 11:36:59 +01:00
parent 5258e90bae
commit e26acc3676
2 changed files with 8 additions and 7 deletions

View File

@@ -7,9 +7,11 @@
namespace pywrap {
PyWrap* PyWrap::wrapper = nullptr;
std::mutex PyWrap::mutex;
PyWrap* PyWrap::GetInstance()
{
std::lock_guard<std::mutex> lock(mutex);
if (wrapper == nullptr) {
std::cout << "Creando instancia" << std::endl;
wrapper = new PyWrap();
@@ -35,15 +37,15 @@ namespace pywrap {
return;
}
std::cout << "No estaba en el mapa" << std::endl;
module = PyImport_ImportModule(moduleName.c_str());
PyObject* module = PyImport_ImportModule(moduleName.c_str());
if (PyErr_Occurred()) {
errorAbort("Could't import module " + moduleName);
}
classObject = PyObject_GetAttrString(module, className.c_str());
PyObject* classObject = PyObject_GetAttrString(module, className.c_str());
if (PyErr_Occurred()) {
errorAbort("Couldn't find class " + className);
}
instance = PyObject_CallObject(classObject, NULL);
PyObject* instance = PyObject_CallObject(classObject, NULL);
if (PyErr_Occurred()) {
errorAbort("Couldn't create instance of class " + className);
}
@@ -89,7 +91,7 @@ namespace pywrap {
errorAbort("Module " + moduleName + " and class " + className + " not found");
}
std::cout << "Clase encontrada" << std::endl;
instance = std::get<2>(item->second);
PyObject* instance = std::get<2>(item->second);
PyObject* result;
if (!(result = PyObject_CallMethod(instance, method.c_str(), NULL)))
errorAbort("Couldn't call method " + method);