# C++ Thread unable to open file

**URL:** <https://forum.hdfgroup.org/t/c-thread-unable-to-open-file/10267>\
**Category:** C++ Users' Group\
**Created:** [September 29, 2022, 1:48am UTC](https://forum.hdfgroup.org/t/c-thread-unable-to-open-file/10267 "2022-09-29T01:48:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![robin.128.456](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@robin.128.456](https://forum.hdfgroup.org/u/robin.128.456)\
**Post date:** [September 29, 2022, 1:48am UTC](https://forum.hdfgroup.org/t/c-thread-unable-to-open-file/10267/1 "2022-09-29T01:48:42Z")

</div>

Hello,  
I am trying to read from two different hdf5 files with two different threads (using std::thread) of a thread pool.  
Each thread has its dedicated hdf5 file.  
However it seems that sometimes the file cannot be opened: According to the visual studio debugger, the error below is raised when the file is opened. Other messages that vary are logged without the debugger.

> Exception thrown at 0x00007FFCBBE5EFE2 (hdf5.dll) in HDFMultiThreadTest.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF

Below is the relevant portion of code that opens the file to read the data. The error does not appear when the thread pool only uses 1 thread. I do not understand why there are thead related issues as each thread has its own hdf5 file.

Is there an element that I missed ? Do you know where this error could come from ?

I am using hdf5 1.12.1 on windows. I can provide the full toy example with the Thread Pool if necessary.

Thanks in advance!

```
double* read_data(int i,int id) {
    using namespace H5;
    double* points;
    // Dataset names are numbers (as strings) in the range 0 to 0 (checked with HDFView)
    std::string dataset_name = std::to_string(i);
    // Format name depending on thread id
    const char* fmt = "benchmark%d.hdf5";
    int sz = std::snprintf(nullptr, 0, fmt, id);
    std::vector<char> buf(sz + 1); // note +1 for null terminator
    std::snprintf(&buf[0], buf.size(), fmt, id);
    std::string NAME_DATA_FILE(buf.begin(), buf.end());
    // Create filename of file to open
    const H5std_string FILE_NAME_BENCHMARK(NAME_DATA_FILE);
    H5std_string DATASET_NAME(dataset_name);
    // Read data
    try {
        H5File file(FILE_NAME_BENCHMARK, H5F_ACC_RDONLY); // <- Debugger error here
        DataSet dataset_points = file.openDataSet(DATASET_NAME);

        DataSpace dataspace_points = dataset_points.getSpace();
        hsize_t dims_points[1];
        hsize_t rank_points = dataspace_points.getSimpleExtentDims(dims_points);

        points = new double[dims_points[0]];
        dataset_points.read(points, PredType::NATIVE_DOUBLE);
    }
    catch (Exception error) {
        error.printErrorStack();
        exit(1);
    }
    return points;
}

```

---

<div class="post-metadata">

**Author:** ![kittisopikulm](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/kittisopikulm/32/1505_2.png) [@kittisopikulm](https://forum.hdfgroup.org/u/kittisopikulm)\
**Post date:** [September 29, 2022, 1:48pm UTC](https://forum.hdfgroup.org/t/c-thread-unable-to-open-file/10267/2 "2022-09-29T13:48:25Z")

</div>

Even if you are accessing two different files, the HDF5 API is not thread safe by default. You can enable a thread safety feature using the option, `--enable-threadsafe`.

[https://docs.hdfgroup.org/hdf5/v1\_12/\_m\_t.html](https://docs.hdfgroup.org/hdf5/v1_12/_m_t.html)

---

<div class="post-metadata">

**Author:** ![robin.128.456](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@robin.128.456](https://forum.hdfgroup.org/u/robin.128.456)\
**Post date:** [September 29, 2022, 2:50pm UTC](https://forum.hdfgroup.org/t/c-thread-unable-to-open-file/10267/3 "2022-09-29T14:50:53Z")

</div>

Thank you for your answer  
I will try to recompile the library with the option  
Just one question: I see that the documentation link uses pthread when the library is compiled in thread safe mode. Is pthread also usable on windows ? (I rarely program with multithreading and more often on linux)

---

<div class="post-metadata">

**Author:** ![derobins](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/derobins/32/1481_2.png) [@derobins](https://forum.hdfgroup.org/u/derobins)\
**Post date:** [September 29, 2022, 4:36pm UTC](https://forum.hdfgroup.org/t/c-thread-unable-to-open-file/10267/4 "2022-09-29T16:36:09Z")

</div>

Under CMake, Windows only uses Win32 threads when thread-safety is selected. This could be changed (PRs accepted!) but we’d have to add testing for that and it’s not a high priority given the low number of people who would use Pthreads on Windows.

---

<div class="post-metadata">

**Author:** ![gheber](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/gheber/32/1919_2.png) [@gheber](https://forum.hdfgroup.org/u/gheber)\
**Post date:** [September 29, 2022, 6:19pm UTC](https://forum.hdfgroup.org/t/c-thread-unable-to-open-file/10267/5 "2022-09-29T18:19:06Z")

</div>

Which HDF5 C++ bindings are you using? I believe [`H5File`](https://docs.hdfgroup.org/hdf5/develop/class_h5_1_1_h5_file.html) & friends are not threadsafe even with a threadsafe C-build. G.

---

<div class="post-metadata">

**Author:** ![robin.128.456](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@robin.128.456](https://forum.hdfgroup.org/u/robin.128.456)\
**Post date:** [September 29, 2022, 6:26pm UTC](https://forum.hdfgroup.org/t/c-thread-unable-to-open-file/10267/6 "2022-09-29T18:26:26Z")

</div>

Thanks for your answers  
Yes I am using H5File…  
Is there an other way to go?

---

<div class="post-metadata">

**Author:** ![derobins](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/derobins/32/1481_2.png) [@derobins](https://forum.hdfgroup.org/u/derobins)\
**Post date:** [September 29, 2022, 6:57pm UTC](https://forum.hdfgroup.org/t/c-thread-unable-to-open-file/10267/7 "2022-09-29T18:57:32Z")

</div>

You should be using the C API from C++ if you want thread-safety. The global lock we use to provide thread-safety is only enabled at the C API level so the C++ components are not thread-safe.

---

<div class="post-metadata">

**Author:** ![robin.128.456](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/r/fbc32d/32.png) [@robin.128.456](https://forum.hdfgroup.org/u/robin.128.456)\
**Post date:** [September 29, 2022, 7:01pm UTC](https://forum.hdfgroup.org/t/c-thread-unable-to-open-file/10267/8 "2022-09-29T19:01:18Z")

</div>

Ok I will try to setup the C API.
