# How to achieve atomic global variables with multiple processes (not threads)?

**URL:** <https://discourse.julialang.org/t/how-to-achieve-atomic-global-variables-with-multiple-processes-not-threads/16626>\
**Category:** Julia at Scale\
**Created:** [October 22, 2018, 12:37pm UTC](https://discourse.julialang.org/t/how-to-achieve-atomic-global-variables-with-multiple-processes-not-threads/16626 "2018-10-22T12:37:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![novoselrok](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/novoselrok/32/5821_2.png) [@novoselrok](https://discourse.julialang.org/u/novoselrok)\
**Post date:** [October 22, 2018, 12:37pm UTC](https://discourse.julialang.org/t/how-to-achieve-atomic-global-variables-with-multiple-processes-not-threads/16626/1 "2018-10-22T12:37:03Z")

</div>

Hello.

I’m writing a dynamically scheduled parallel algorithm, which spawns new processes when other processes run out of work. I’m keeping a counter of currently running processes so I don’t spawn more than max available processes. I would like to keep this counter atomic (to prevent data races).

My thought process so far:

- Process 0 spawns 4 new processes (P1, P2, P3, P4)
- P0 holds a counter which P1-4 update
- P1-4 send their updates through a remote channel, and P0 updates the counter
- P1-4 want to access the counter, but they can’t just call `remotecall_fetch` since it might change in the meantime

How do I achieve atomicity of this global counter using Julia processes?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 22, 2018, 2:31pm UTC](https://discourse.julialang.org/t/how-to-achieve-atomic-global-variables-with-multiple-processes-not-threads/16626/2 "2018-10-22T14:31:06Z")

</div>

> [@novoselrok](#):
>
> How do I achieve atomicity of this global counter using Julia processes?

Keep the counter on a single process and have the other ones access the counter through it.

---

<div class="post-metadata">

**Author:** ![novoselrok](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/novoselrok/32/5821_2.png) [@novoselrok](https://discourse.julialang.org/u/novoselrok)\
**Post date:** [October 22, 2018, 2:57pm UTC](https://discourse.julialang.org/t/how-to-achieve-atomic-global-variables-with-multiple-processes-not-threads/16626/3 "2018-10-22T14:57:04Z")

</div>

Ok, so if I keep the counter on process 0, I can use `remotecall_fetch` on other processes without problems?
