When calling std::thread::join() program hangs [closed]

  c++, multithreading

Here i have function to start thread. When program reach ThreadFindFile.join() my program hangs.
I thought there was a thread deadlock problem. But then i found this bug and dont understand how solve it. How can i fix it? And what is the main problem why join() dont work?

std::atomic<bool>StopThread=FALSE;
void Finder::StartThread(CString path)
{
    if( ThreadFindFile.joinable() )
    {
        StopThread = TRUE;
        ThreadFindFile.join();
        StopThread = FALSE;
    }
    imageIndex = 0;
    Tmp.ListView.DeleteAllItems();
    ThreadFindFile=std::thread(  (&Finder::findFile),this , path, std::ref(imageIndex) );
}

Update. I understand that join() wait this thread, but in console message told me that thread ends succesful. So, why the problem hangs?

Update code of function that calling in thread

void Finder::findFile( CString szPath, int& i )
{
    CFindFile FileSearch;
    CString S = szPath + TEXT( "*.*" );
    BOOL bFlag = FileSearch.FindFile( S );
    if( !bFlag )
    {
        MessageBox( ListView_hWnd, TEXT( "Error" ), TEXT( "File not found" ), 0 );
    }
    else
    {
        do
        {
            if( FileSearch.IsDots() )
            {
                continue;
            }
            else
            {
                if( StopThread )
                {
                    return;
                }
                if( FileSearch.IsDirectory() )
                {
                    findFile( FileSearch.GetFilePath(), i );
                }
                view_List( FileSearch.GetFileName(), i, FileSearch.GetFilePath() );
                i++;
            }
        } while( FileSearch.FindNextFileW() );
        FileSearch.Close();
    }
    Tmp.ListView.SetImageList( hSmall, 1 );
}
                
void Finder::view_List( CString name, int i, CString path )
{
    LVITEM lvItem;
    SHFILEINFOW lp{};
    CString nameWithoutEx = std::get<0>( split( name ) );
    CString extention = std::get<1>( split( name ) );
    lvItem.mask = LVIF_IMAGE | LVIF_TEXT;
    lvItem.state = 0;
    lvItem.stateMask = 0;
    lvItem.iItem = i;
    lvItem.iImage = i;
    lvItem.iSubItem = 0;
    lvItem.pszText = const_cast< LPWSTR >( nameWithoutEx.GetString() );
    lvItem.cchTextMax = nameWithoutEx.GetLength();
    Tmp.ListView.InsertItem( &lvItem );
    Tmp.ListView.SetItemText( i, 1, extention.GetString() );
    Tmp.ListView.SetItemText( i, 2, path.GetString() );
    DWORD num = GetFileAttributesW( path.GetString() );
    SHGetFileInfoW( path.GetString(), num, &lp, sizeof( lp ),
                    SHGFI_SYSICONINDEX | SHGFI_ICON | SHGFI_USEFILEATTRIBUTES );
    hSmall.AddIcon( lp.hIcon );
    DestroyIcon( lp.hIcon );
}

Source: Windows Questions C++

LEAVE A COMMENT