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. 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.
When I load it from other C++ and C# projects it works fine, and when calling other methods it works well too. The problem occurs whe I call 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: