Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.3 kB
17
Indexable
void getFilesMultithreadedly(std::wstring path)
{
    try
    {
        ++counter;

        WIN32_FIND_DATA data;

        FSNodes local_nodes;

        std::wstring local_path = std::wstring((path + std::wstring(L"\\*")).c_str());

        HANDLE hfind = FindFirstFile(local_path.data(), &data);

        if (hfind != INVALID_HANDLE_VALUE)
        {
            do
            {
                if (std::wstring filename = data.cFileName; filename != L"." && filename != L".." && filename != L"")
                {
                    if (!(data.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY))
                    {
                        if (!(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
                        {
                            local_nodes.emplace_back(std::make_unique<File>(local_path + filename));
                        }
                        else
                        {
                            local_nodes.emplace_back(std::make_unique<Folder>(filename));

                            {
                                std::scoped_lock lock(pool_mutex);

                                std::wstring formatted_path = formatPath(std::move(filename), path);

                                local_nodes.emplace_back(std::make_unique<Folder>(formatted_path));

                                pool.enqueue([formatted_path]
                                    {
                                        getFilesMultithreadedly(std::move(formatted_path));
                                    }
                                );

                            }

                        }
                    }
                }
            } while (FindNextFile(hfind, &data));

        }

        FindClose(hfind);

        {
            std::scoped_lock lock(result_mutex);

            result.insert(result.end(), std::make_move_iterator(local_nodes.begin()), std::make_move_iterator(local_nodes.end()));
        }

        --counter;

        if (counter == 0)
        {
            std::unique_lock lock(conditional_variable_mutex);
            cv.notify_all(); // all of the processes are over, the function has done it's job(this method is not working)
        }

    }
    
    catch (std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
}