ROS

C++ - Filesystem

Posted by Rico's Nerd Cluster on April 14, 2024

std::filesystem::create_directory(path)

Thread safety:

  • std::filesystem::create_directory(path); under the hood it issues a single POSIX mkdir() (or the platform equivalent).
    • If the directory doesn’t exist, one thread’s call will succeed and return true;
    • Any concurrent or subsequent calls will see that the directory already exists and return false
  • Below is a TOCTOU (Time Of Check To Time Of Use) race:
1
2
3
if (!std::filesystem::exists(path)) {
    std::filesystem::create_directory(path);
}