HDF5 File Locking Mechanism

Hi all,
I’ve been experimenting with the HDF5 file locking mechanism and encountered some unexpected behavior (hdf5 1.14.4). Below is the code snippet I used to test the file locking:

#include "hdf5.h"
#include "H5Cpp.h"

#include <cstdio>
#include <iostream>
   // Create file access property list with file locking disabled
   bool isLockedFile = false; // Seems to work upside down
   H5::FileAccPropList accessPropList;
   accessPropList.setFileLocking(isLockedFile, true);

   // Create an HDF5 file with the specified access properties
   H5::H5File file = H5::H5File("testopen.h5", H5F_ACC_TRUNC, H5::FileCreatPropList::DEFAULT, accessPropList);

   //Check locking property
   // Check the locking property of the created file
   const H5::FileAccPropList pList(file.getAccessPlist());
   hbool_t isLocked = true;
   hbool_t isIgnored = true;
   pList.getFileLocking(isLocked, isIgnored);
   std::cout << "isLocked: " << (isLocked ? "true" : "false") << std::endl;

   // Attempt to open the same file again
   H5::H5File file2;
   try
   {
       file2.openFile("testopen.h5", H5F_ACC_RDONLY);
       std::cout << "File opened successfully." << std::endl;
   }
   catch (const H5::FileIException& e)
   {
       std::cerr << "Failed to open file: " << e.getCDetailMsg() << std::endl;
   }

   // Close the files
   file2.close();
   file.close();

• When isLockedFile is set to false, the file locking mechanism is supposed to be disabled. However, the code successfully opens the file twice, which is unexpected.
• When isLockedFile is set to true, the file locking mechanism is supposed to be enabled. Surprisingly, the code fails to open the file a second time, which contradicts the expected behavior.

Has anyone else encountered this behavior? Is there something I’m missing in the configuration or usage of the HDF5 file locking mechanism?

Hi Jonathan, please take a look at

and check your environment variable setting. That might explain the unexpected behaviors.