繁体   English   中英

单例和.dll中的std :: thread块

[英]std::thread blocks in a singleton and .dll

我已经阅读了很多有关此内容的信息,但尚未找到任何解决方案。

我的问题是,当我尝试执行此操作时,我失去了控制。

hiloEscucha = std::thread(&PlayerClientSingleton::Run, this);

我正在与一个在多线程环境中实现单例模式的类一起工作。 我的项目被编译为DLL库。 除了创建线程之外,其他一切都正常。

我已经尝试过此( 链接 ),并且此示例在独立的控制台应用程序中可以正常工作,但是我的项目作为DLL库运行。

当我从其他C ++和C#项目中加载它时,它运行良好,而在调用其他方法时,它也运行良好。 我调用std::thread时出现问题。

这是我的代码中最重要的部分:

class PlayerClientSingleton : public PlayerClient {
public:
    static PlayerClientSingleton* instance(void) {
        std::call_once(m_onceFlag, [] { m_instance = new PlayerClientSingleton; });
        return m_instance;
    }

    //Initialize is called from the .dll initialization (Dllmain)
    bool initialize(void) { 
        runThread();
    }

    void runThread(void) {
        m_thread = std::thread(&PlayerClientSingleton::Run, this);

        if (m_thread.joinable()) {
            m_thread.detach();
        }
    }

    static PlayerClientSingleton* m_instance;
    static std::once_flag         m_onceFlag;
    std::thread                   m_thread;
}

class PlayerClient {
protected:
    virtual void Run() {
        while (!m_threadEdn) {
            doSomething();
        }
    }
}

知道为什么它不起作用吗?

我认为这与您尝试从DllMain创建线程有关,另请参见:

在DllMain中创建线程?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM