I'm using std::async
in an app where I want to run a function asynchronously, and from that function use SendMessage
function to talk to the UI from the worker thread. The following is an extract from an MFC test app that demonstrates what I'm doing:
LRESULT CStdAsyncProjDlg::OnUserPlusOne(WPARAM, LPARAM)
{
MessageBox("Message", "Hello World Again", MB_OK);
return 0;
}
// Function that contains the async work.
void TestAsync(HWND hDlg)
{
// Send a message to the UI from this worker. The WM_USER + 1 message
// handler is CStdAsyncProjDlg::OnUserPlusOne
SendMessage(hDlg, WM_USER + 1, 0, 0);
}
// This event is fired when a button is pressed on the MFC dialog.
void CStdAsyncProjDlg::OnBnClickedButton1()
{
MessageBox("Message", "Hello World", MB_OK);
std::async(std::launch::async, TestAsync, m_hWnd);
}
In Visual Studio 2013 the above code works as expected. When I press the button I get a message box that says "Hello World", and once I click OK on that message box I get the other message box that says "Hello World Again".
The issue I'm facing is once I migrate the above code to Visual Studio 2015 compiler, the app hangs after the SendMessage
function call.
Reading online (answer to this question) it mentions that the destructor for std::future
blocks. I've changed the code in Visual Studio 2015 to store the returned std::future
from std::async
and that seems to fix the issue (the app doesn't hang and I get the second message box). However looking at the std::future
code I can't seem to see anything different between Visual Studio 2013 and 2015. So my question is has anything changed in the way std::async
works that would cause such behaviour?
Thanks