# Safely Analyzing Multiple Files with Multiple Threads

**URL:** <https://discourse.julialang.org/t/safely-analyzing-multiple-files-with-multiple-threads/99738>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [June 1, 2023, 5:59pm UTC](https://discourse.julialang.org/t/safely-analyzing-multiple-files-with-multiple-threads/99738 "2023-06-01T17:59:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gus-pendleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus-pendleton/32/49241_2.png) [@gus-pendleton](https://discourse.julialang.org/u/gus-pendleton)\
**Post date:** [June 1, 2023, 5:59pm UTC](https://discourse.julialang.org/t/safely-analyzing-multiple-files-with-multiple-threads/99738/1 "2023-06-01T17:59:04Z")

</div>

Hello!

I am writing functions which read multiple files of the same type, perform some analysis, and write results to a single object. I am unsure if the current way it is written is “safe” for multithreading. Here is a simple example:

Let’s say you have three .txt files:  
file1.txt  
file2.txt  
file3.txt

Each file contains some text.

My function would look like this:

```julia
# Defining the function
function read_that_file(filepaths::Vector{String})
    n_files = length(filepaths)
    results = Vector{String}(undef, n_files)
    Threads.@threads for i in 1:n_files
                         results[i] = open(io->read(io, String), filepaths[i])
                     end
     return results
end

# Running the function on a vector of filepaths
read_that_file(["file1.txt","file2.txt","file3.txt"])

```

I’m unsure whether I need locks in this scenario. Multiple threads should never try to access the same file, because they shouldn’t be assigned the same index. Similarly, multiple threads will write to the same results vector, but never at the same index.

The last, kinda weird use case, would be if someone provided the same filename in the filepaths vector, for example:

```julia
read_that_file(["file1.txt", "file1.txt"])

```

At which point those rules would be broken, which might cause an issue?

Thanks!

Gus

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 1, 2023, 7:00pm UTC](https://discourse.julialang.org/t/safely-analyzing-multiple-files-with-multiple-threads/99738/2 "2023-06-01T19:00:58Z")

</div>

I think everything is fine, if the files are open read-only.

---

<div class="post-metadata">

**Author:** ![gus-pendleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus-pendleton/32/49241_2.png) [@gus-pendleton](https://discourse.julialang.org/u/gus-pendleton)\
**Post date:** [June 1, 2023, 7:30pm UTC](https://discourse.julialang.org/t/safely-analyzing-multiple-files-with-multiple-threads/99738/3 "2023-06-01T19:30:43Z")

</div>

Thanks!
