# Mutable structs in parallel, is this a bug?

**URL:** https://discourse.julialang.org/t/mutable-structs-in-parallel-is-this-a-bug/7694
**Category:** General Usage
**Created:** [December 11, 2017, 5:03pm UTC](https://discourse.julialang.org/t/mutable-structs-in-parallel-is-this-a-bug/7694 "2017-12-11T17:03:37Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)
#### Post date: [December 11, 2017, 5:03pm UTC](https://discourse.julialang.org/t/mutable-structs-in-parallel-is-this-a-bug/7694/1 "2017-12-11T17:03:37Z")

</div>

Hello,

I ran into this today, if you have a module with a mutable struct:

```julia
module container2
export cont2
mutable struct cont2
 a
end
end

```

When the fields of the mutable struct are changed on the master node, this change is not detected on the other procs:

```julia
addprocs(3)

3-element Array{Int64,1}:
 2
 3
 4

using container2

bla=cont2((0.,0.))
container2.cont2((0.0, 0.0))

￼
@sync for ip in procs()
    @spawnat ip @show bla.a
end

bla.a = (0.0, 0.0)
	From worker 2:	bla.a = (0.0, 0.0)
	From worker 4:	bla.a = (0.0, 0.0)
	From worker 3:	bla.a = (0.0, 0.0)

bla.a=((1.,1.))

@sync for ip in procs()
    @spawnat ip @show bla.a
end
bla.a = (1.0, 1.0)
	From worker 2:	bla.a = (0.0, 0.0)
	From worker 4:	bla.a = (0.0, 0.0)
	From worker 3:	bla.a = (0.0, 0.0)

```

That was surprising behavior to me, and it can lead to bugs. Is this expected behavior?

Thank you

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [December 11, 2017, 6:11pm UTC](https://discourse.julialang.org/t/mutable-structs-in-parallel-is-this-a-bug/7694/2 "2017-12-11T18:11:52Z")

</div>

How should the remote processes know that the value has been updated? Use `@everywhere`:

```julia
julia> bla = container2.cont2((0., 0.))
container2.cont2((0.0, 0.0))

julia> @sync for ip in procs()
           @spawnat ip @show bla.a
       end
bla.a = (0.0, 0.0)
	From worker 2:	bla.a = (0.0, 0.0)
	From worker 3:	bla.a = (0.0, 0.0)
	From worker 4:	bla.a = (0.0, 0.0)

julia> bla.a = ((1., 1.))
(1.0, 1.0)

julia> @sync for ip in procs()
           @spawnat ip @show bla.a
       end
bla.a = (1.0, 1.0)
	From worker 2:	bla.a = (0.0, 0.0)
	From worker 4:	bla.a = (0.0, 0.0)
	From worker 3:	bla.a = (0.0, 0.0)

julia> @everywhere bla.a = ((2., 2.))

julia> @sync for ip in procs()
           @spawnat ip @show bla.a
       end
bla.a = (2.0, 2.0)
	From worker 2:	bla.a = (2.0, 2.0)
	From worker 4:	bla.a = (2.0, 2.0)
	From worker 3:	bla.a = (2.0, 2.0)

```

---

<div class="post-metadata">

### Author: ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)
#### Post date: [December 11, 2017, 6:30pm UTC](https://discourse.julialang.org/t/mutable-structs-in-parallel-is-this-a-bug/7694/3 "2017-12-11T18:30:53Z")

</div>

The point is that `@spawnat` interpolates the variables from `Main` to the other processes (`@everywhere` does not for example). So the first call defines `bla` everywhere, by “sending” `bla` from the master.

In subsequent calls, `@spawnat` checks, if `bla` has changed (if the variable was rebound, for example), it re-interpolates the new variable. But if only the contents have mutated, the change is not detected and the variable is not re-interpolated (for efficiency reasons I am guessing). I found it surprising, I thought it was always synchronizing the variable. Is that documented?
