# JLD on Multithreading

**URL:** <https://discourse.julialang.org/t/jld-on-multithreading/12632>\
**Category:** General Usage\
**Tags:** jld, hdf5, multithreading\
**Created:** [July 24, 2018, 10:01am UTC](https://discourse.julialang.org/t/jld-on-multithreading/12632 "2018-07-24T10:01:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidBerghaus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidberghaus/32/4623_2.png) [@DavidBerghaus](https://discourse.julialang.org/u/DavidBerghaus)\
**Post date:** [July 24, 2018, 10:01am UTC](https://discourse.julialang.org/t/jld-on-multithreading/12632/1 "2018-07-24T10:01:33Z")

</div>

I am calculating matrix entries through multithreading and now would like to save the matrix to a file because it gets too big for my RAM.

I read that the package JLD is most common for saving Julia variables.

But when I am now trying to save the multi threaded entries to a file:

```julia
using JLD
function test()
	save("test2.jld", "test", 1) #create file
	f = jldopen("test2.jld", "r+")
	Threads.@threads for i = 1:10
		for j = 1:10
			x = i + j
			write(f, "$i $j", x)
		end
	end
end

```

I get lots of errors, the first of them is:

> Exception: EXCEPTION\_ACCESS\_VIOLATION at 0x6dad7bb7 – HDF5-DIAG: Error detected in HDF5 (1.8.13) at 0x6dad7bb7

I also tried to create a file for each matrix entry, but that did not work neither.

Does anyone know how to fix this or can recommend another way to save the matrix?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![traktofon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/traktofon/32/591_2.png) [@traktofon](https://discourse.julialang.org/u/traktofon)\
**Post date:** [July 24, 2018, 11:26am UTC](https://discourse.julialang.org/t/jld-on-multithreading/12632/2 "2018-07-24T11:26:45Z")

</div>

Writing in parallel to a single file is always dangerous, because those writes must be properly coordinated. I’m not sure if the Julia/JLD implementation does that. To be on the safe side, I suggest to serialize the `write`s. To do so, at the start of your function create a lock:

```julia
sl = Threads.SpinLock()

```

And then surround the `write` call with calls to `lock` and `unlock`:

```julia
lock(sl)
write(...)
unlock(sl)

```

In production code the call to `write` should probably be in a `try` block and the `unlock` call in a `finally` clause.

---

<div class="post-metadata">

**Author:** ![DavidBerghaus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidberghaus/32/4623_2.png) [@DavidBerghaus](https://discourse.julialang.org/u/DavidBerghaus)\
**Post date:** [July 24, 2018, 11:55am UTC](https://discourse.julialang.org/t/jld-on-multithreading/12632/3 "2018-07-24T11:55:11Z")

</div>

Okay, that works perfectly, thank you very much!
