# How to make a wrapper around a non-thread safe C library thread-safe

**URL:** <https://discourse.julialang.org/t/how-to-make-a-wrapper-around-a-non-thread-safe-c-library-thread-safe/28950>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [September 19, 2019, 2:59pm UTC](https://discourse.julialang.org/t/how-to-make-a-wrapper-around-a-non-thread-safe-c-library-thread-safe/28950 "2019-09-19T14:59:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabiangans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabiangans/32/2624_2.png) [@fabiangans](https://discourse.julialang.org/u/fabiangans)\
**Post date:** [September 19, 2019, 2:59pm UTC](https://discourse.julialang.org/t/how-to-make-a-wrapper-around-a-non-thread-safe-c-library-thread-safe/28950/1 "2019-09-19T14:59:35Z")

</div>

Dear all,

I am looking for hints how to make the [NetCDF.jl](https://github.com/JuliaGeo/NetCDF.jl) prepared for the new threading in Julia 1.3. The package is a wrapper around a C library which is not thread-safe, so I think one has to make sure that only one thread at a time makes a ccall into the linked library and other threads have to wait accordingly. Is it desirable to do the locking on the library-level and if yes, are there examples where this is already implemented?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 19, 2019, 3:51pm UTC](https://discourse.julialang.org/t/how-to-make-a-wrapper-around-a-non-thread-safe-c-library-thread-safe/28950/2 "2019-09-19T15:51:56Z")

</div>

You should generally leave that to the user unless you are sure that almost all users would like to use multiple threads and you know exactly how they’ll use it.

---

<div class="post-metadata">

**Author:** ![fabiangans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabiangans/32/2624_2.png) [@fabiangans](https://discourse.julialang.org/u/fabiangans)\
**Post date:** [September 19, 2019, 7:59pm UTC](https://discourse.julialang.org/t/how-to-make-a-wrapper-around-a-non-thread-safe-c-library-thread-safe/28950/3 "2019-09-19T19:59:55Z")

</div>

Thanks, so I will just add a warning to the documentation of the package saying it should not be used in a threaded context (or with caution).
