Yeh, it seems to be the same thing. I checked under Linux with GCC and it works. On my point of view, Visual C++ runtime after exiting the main() destroys all the threads then all the global variables, instead of the opposite.
dchabaud
Posts
-
Call to std::thread::join() in the destructor of a global variable -
Call to std::thread::join() in the destructor of a global variableNot exactly, the destructor of the global variable sets an atomic to tell the thread to exit.
-
Call to std::thread::join() in the destructor of a global variableI have the following code and when running it stays blocked on the join(). So, is it possible to call std::thread::join() in the destructor of a global variable?
void MyTimerFunction();
class MyGlobal
{
public:
std::atomic<bool> m_Flag;
std::thread m_GlobalThread;MyGlobal() { m\_Flag = true; m\_GlobalThread = std::thread(MyTimerFunction); } ~MyGlobal() { m\_Flag = false; if (m\_GlobalThread.joinable()) m\_GlobalThread.join(); std::cout << "MyGlobal destroyed" << std::endl; }
};
MyGlobal Test;
void MyTimerFunction()
{
do
{
std::this_thread::sleep_for(std::chrono::seconds{10});
if (!Test.m_Flag)
break;std::cout << "New tick" << std::endl; } while (true); std::cout << "Exit MyTimerFunction" << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Main thread sleeping..." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds{15});
std::cout << "Main thread awaking..." << std::endl;std::cout << "Main thread returning..." << std::endl; return 0;
}
-
CSocket::Connect returns WSAEISCONNI have a MFC serveur and a MFC client which work fine in most cases except 1 (the client and the serveur are in different Active Directory trees on the same network). On the client I use : m_pConnectionSocket = new CSocket(); if (m_pConnectionSocket && m_pConnectionSocket->Create()) { if (m_pConnectionSocket->Connect(m_strLicenseServerName, TCP_PORT_3)) { ... } } In the case it does not work Connect returns WSAEISCONN error which make no sense on my point of view. Any idea ?