C ++ adapts library for multithreading

I work with libconfig ++ and threads to create a small server application. The fact is that libconfig ++ is not thread safe , so my idea is to create another class that acts like a wrapper with Mutex, something like this:

class app_config { public: app_config(); /* Here be my problems. */ void set(); void get(); virtual ~app_config(); private: Config cfg; boost::mutex *cfg_mutex; }; 

Now all is well, until I understand that libconfig supports many types for its variables. And so when our main character (s) finds himself in search of any C ++ guru with a good heart who wants to show him any way to make it work.

In essence, the get and set functions require the variable std::string or char* path , containing the path to the configuration file variable (I would not think to use it either) and the type of the return value (or the second argument in the case of set ) should change ...

As always, any help would be appreciated.

Julian

+4
source share
2 answers

You can also use this approach. I think it’s harder to miss, and therefore better. The Libconfig instance is a private member inside the shell and cannot be accessed without locking.

 #include <boost/thread/mutex.hpp> #include <boost/thread/locks.hpp> #include <boost/shared_ptr.hpp> class Libconfig { public: int Get(const char*) { return 0; } }; class LibConfLock; class LibconfMultithreadingWrapper { friend class LibConfLock; public: LibconfMultithreadingWrapper() :m_Libconfig(new Libconfig()) ,m_Mutex(new boost::mutex()) {} private: boost::shared_ptr<Libconfig> m_Libconfig; boost::shared_ptr<boost::mutex> m_Mutex; }; class LibConfLock { public: LibConfLock(const LibconfMultithreadingWrapper& wrapper) :m_Libconfig(wrapper.m_Libconfig) ,m_Mutex(wrapper.m_Mutex) ,m_Lock(new LockType(*m_Mutex)) {} Libconfig& GetLibconf() const { return *m_Libconfig; } private: typedef boost::lock_guard<boost::mutex> LockType; boost::shared_ptr<Libconfig> m_Libconfig; boost::shared_ptr<boost::mutex> m_Mutex; boost::shared_ptr<LockType> m_Lock; }; int main() { LibconfMultithreadingWrapper wrapper; int i = LibConfLock(wrapper).GetLibconf().Get("hallo"); return i; } 
+1
source

You can write a decorator class that forwards all function calls to a private instance of libconfig. This means that you need to add all the functions that you want to use for your decorator. Another possibility is to forward the libconfig call to the class that performs the actual lock.

 #include <boost/thread/mutex.hpp> #include <boost/thread/locks.hpp> #include <boost/bind.hpp> class MultithreadingWrapper { public: template <class V, class T> V ExecuteThreadSaveWithReturn(T func) { boost::lock_guard<boost::mutex> l(m_Mutex); return func(); } template <class T> void ExecuteThreadSave(T func) { boost::lock_guard<boost::mutex> l(m_Mutex); func(); } private: boost::mutex m_Mutex; }; void f() {} void f(int) { } int f(int, int) { return 0; } int main() { MultithreadingWrapper wrapper; wrapper.ExecuteThreadSave(boost::bind(f)); wrapper.ExecuteThreadSave(boost::bind(f, 1)); int i = wrapper.ExecuteThreadSaveWithReturn<int>(boost::bind(f, 1, 1)); return i; } 
+1
source

Source: https://habr.com/ru/post/1390951/


All Articles