C++为多线程调整库

C++ adapting a library for multithreading

本文关键字:调整 多线程 C++      更新时间:2023-10-16

我正在使用libconfig++和线程来制作一个小型服务器应用程序。重点是,libconfig++不是线程安全的,所以我的想法是创建另一个类,它充当Mutex的包装器,类似于这样:

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

现在,这一切都很好,直到我意识到libconfig支持其变量的大量类型。当我们的主人公(我)发现自己在寻找任何一位心地善良的C++大师时,他愿意向他展示任何实现这一目标的方法。

从本质上讲,getset函数需要一个std::stringchar* path变量,其中包含配置文件变量的路径(我不介意使用任何一个),并且返回类型(或set的第二个参数)应该不同。。。

一如既往,我们将不胜感激。

Julian

您也可以使用这种方法。我认为它更难误用,因此更优越。Libconfig实例是包装器内部的私有成员,没有锁就无法访问。

#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;
}

您可以编写一个装饰器类,将所有函数调用转发到私有libconfig实例。这意味着您需要将要使用的所有函数添加到装饰器中。另一种可能性是将对libconfig的调用转发给执行实际锁定的类。

#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;
}