# Managing Single and Multithreaded Implementations

**URL:** <https://discourse.julialang.org/t/managing-single-and-multithreaded-implementations/30083>\
**Category:** Performance\
**Created:** [October 19, 2019, 3:00pm UTC](https://discourse.julialang.org/t/managing-single-and-multithreaded-implementations/30083 "2019-10-19T15:00:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjkelly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sjkelly/32/9280_2.png) [@sjkelly](https://discourse.julialang.org/u/sjkelly)\
**Post date:** [October 19, 2019, 3:00pm UTC](https://discourse.julialang.org/t/managing-single-and-multithreaded-implementations/30083/1 "2019-10-19T15:00:31Z")

</div>

As a library maintainer what are some good idioms for managing implementations of single threaded and multi threaded algorithm implementations?

Yesterday I implemented an algorithm three ways, with channels, locks, and thread-local arrays. For my algorithm (isosurface extraction), locks give the best overall performance on multiple threads. However, in the single thread case I notice 50% -15% slower performance. My first thought was to implement two loops for the single thread and multi threaded cases. If I do this, I am left with two implementations.

What is the expectation for package maintainers? Is it reasonable to expect users will run julia with \>1 thread in the future, and keep the code terse? Is it worth maintaining two implementations to keep good single thread performance?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [October 19, 2019, 4:07pm UTC](https://discourse.julialang.org/t/managing-single-and-multithreaded-implementations/30083/2 "2019-10-19T16:07:13Z")

</div>

I’ve been maintaining two versions, and using a key word argument `threaded::Bool = Threads.nthreads() > 1` to determine if it’s threaded.

It’ll default to using multiple threads if they have several, but I still want to give them (or myself) the option to use a single thread, in case they want to call that function in parallel from multiple threads, rather that run the function in parallel itself.
