std::thread in MFC

Flaviu_ 1,031 Reputation points
2024-02-15T19:00:11.5333333+00:00

It is ok to finish a MFC app while a std::thread is still working? Code:

void CMyMfcView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	std::thread t(&CMyMfcDoc::SomeMethod, GetDocument());
	t.detach();
	CRichEditView::OnLButtonDblClk(nFlags, point);
}

void CMyMfcDoc::SomeMethod()
{
	// do some long time work, maybe even if MFC application (and main thread) is done
}

If is not ok, how can I assure my MFC application is not stopped until t thread is done?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,891 questions
{count} votes

Accepted answer
  1. RLWA32 47,696 Reputation points
    2024-02-18T22:12:53.9+00:00

    An even simpler alternative is to use a std::thread member variable in the MFC application class instead of a HANDLE. Of course the thread would still need to be told it is time to terminate. The advantage here is that only the native C++ methods are used to wait for the thread to end. So using your example (assuming MFC SDI), the code to start the thread could look like this -

    void CSDIThreadEndView::OnLButtonDblClk(UINT nFlags, CPoint point)
    {
        // TODO: Add your message handler code here and/or call default
        CSDIThreadEndApp* pApp = (CSDIThreadEndApp*)AfxGetApp();
        pApp->m_stdThread = std::thread(&CSDIThreadEndDoc::SomeMethod, GetDocument());
        CRichEditView::OnLButtonDblClk(nFlags, point);
    }
    

    and the application class ExitInstance function could contain -

    int CSDIThreadEndApp::ExitInstance()
    {
        if (m_stdThread.joinable())
            m_stdThread.join();
    
    // rest of ExitInstance function
    }
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. RLWA32 47,696 Reputation points
    2024-02-15T19:48:44.1166667+00:00

    Calling std::thread.detach() avoids the problem of not calling join() before the std::thread object destructor is called but the thread is still running. When your application exits Windows will summarily terminate the thread. It is unknown whether or not that would be a problem since we don't know what the thread is doing when it is terminated. If it is operating on an MFC object its possible that the object could be destroyed while your thread is active (and unaware it is working with an invalid object) unless you have taken precautions to prevent this. If you want to wait for the thread before exiting the application then your code should provide some method to signal the thread that it should exit and you can wait on the thread HANDLE in the MFC application object's ExitInstance method.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.