[英]std::thread blocks in a singleton and .dll
I've read a lot about this, but I haven't found any solution yet. 我已经阅读了很多有关此内容的信息,但尚未找到任何解决方案。
My problem is that when I try to do this, I loose control. 我的问题是,当我尝试执行此操作时,我失去了控制。
hiloEscucha = std::thread(&PlayerClientSingleton::Run, this);
I'm working with a class implementing the singleton pattern in a multithread environment. 我正在与一个在多线程环境中实现单例模式的类一起工作。 My project is compiled to a DLL library.
我的项目被编译为DLL库。 Everything works fine except thread creation.
除了创建线程之外,其他一切都正常。
I've tried this ( link ) and this example works fine in a stand alone console app, but my project is running as a DLL library. 我已经尝试过此( 链接 ),并且此示例在独立的控制台应用程序中可以正常工作,但是我的项目作为DLL库运行。
When I load it from other C++ and C# projects it works fine, and when calling other methods it works well too. 当我从其他C ++和C#项目中加载它时,它运行良好,而在调用其他方法时,它也运行良好。 The problem occurs whe I call
std::thread
. 我调用
std::thread
时出现问题。
Here is the most important pieces of my code: 这是我的代码中最重要的部分:
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();
}
}
}
Any idea why it does not work? 知道为什么它不起作用吗?
I think it has to do with the fact that you try to create the thread from DllMain, see also: 我认为这与您尝试从DllMain创建线程有关,另请参见:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.