Question

What I'm looking for is a way to block the thread until it succeeds to delete a folder using boost-filesystem.

If a folder contains a file that is in use, it should block and continue only after the file is released and was deleted successfully.

Was it helpful?

Solution

Using boost filesystem operation remove_all() should do the job for you normally.

I'd guess you're referring to the problem that this operation might throw an exception or return an error instead of waiting, when a single file can't be removed due to concurrent access. You can simply solve this by catching the exception and put the try/catch block inside a loop that runs until the whole operation was done without any error or exception:

boost::filesystem::path dirToRemove("SomeDirectoryToRemove");
bool completed = false;
while(!completed)
{
    try
    {
        boost::filesystem::remove_all(dirToRemove);
        completed = true;
    }
    catch(...) 
    {
        // put a sleep() call or other blocking operation here, to give other 
        // threads a chance to run, while this one waits to get rid of the error
        // condition.
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top