# Program Structure when muliple routines need to access State

**URL:** <https://discourse.julialang.org/t/program-structure-when-muliple-routines-need-to-access-state/42918>\
**Category:** Performance\
**Tags:** question, performance, multithreading\
**Created:** [July 12, 2020, 3:04am UTC](https://discourse.julialang.org/t/program-structure-when-muliple-routines-need-to-access-state/42918 "2020-07-12T03:04:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pyrex41](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@pyrex41](https://discourse.julialang.org/u/pyrex41)\
**Post date:** [July 12, 2020, 3:04am UTC](https://discourse.julialang.org/t/program-structure-when-muliple-routines-need-to-access-state/42918/1 "2020-07-12T03:04:16Z")

</div>

I have a program where I have an input feed (via ZMQ) that constantly updates an Array to maintain current state. I have other processes (either separate Tasks via @async or maybe on other threads via multithreading, still a design choice in flux) which will need to periodically access the most current state of this array.

My current design simply maintains this State Array as a global variable, and copies it for each task that is generated to process state. I was reading through the performance tips about not using global variables, but I am not seeing any other way to structure the program. This seems like it must be a pretty common design pattern, though, so I was wondering if there is a better approach to solving this problem in more performant way? Performance, in this context, means doing the calculations with the most current State as fast as possible.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [July 12, 2020, 5:45am UTC](https://discourse.julialang.org/t/program-structure-when-muliple-routines-need-to-access-state/42918/2 "2020-07-12T05:45:16Z")

</div>

it seems like a work for a lock. check this thread: [Poor performance of `lock(l) do` closures, caused by poor performance of capturing closures](https://discourse.julialang.org/t/poor-performance-of-lock-l-do-closures-caused-by-poor-performance-of-capturing-closures/42067)

---

<div class="post-metadata">

**Author:** ![pyrex41](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@pyrex41](https://discourse.julialang.org/u/pyrex41)\
**Post date:** [July 13, 2020, 1:24pm UTC](https://discourse.julialang.org/t/program-structure-when-muliple-routines-need-to-access-state/42918/3 "2020-07-13T13:24:01Z")

</div>

Thank you. The `@lock` macro helps solve a very specific problem, and I was not at all aware of it.

As I dug into the this issue, I discovered I did not really understand how the `$` interpolation for `Threads.@spawn` works for structures (more discussion [here](https://discourse.julialang.org/t/interpolation-into-threads-spawn-macro-fails-for-certain-kinds-of-structs/42964/4)). At any rate, now I’m trying to find a way to measure the impact of using lots of copy operations when all I want to do is read state for a very brief amount of time (but ensure that, for a few milliseconds, I see the full state exactly as it was when my calculation started, no updates). But during this, I may be spawning another process that _does_ need the most up-to-date state…so I cannot lock updates to state during my calculation…

Looks like I need each spawned process to have it’s own copy. But given that only a few changes will happen during the spawned process, still seems like a waste to copy everything.
