簡體   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