# How do you preform multiple actions in a thread safe way?

**URL:** <https://discourse.julialang.org/t/how-do-you-preform-multiple-actions-in-a-thread-safe-way/88049>\
**Category:** General Usage\
**Tags:** question, multithreading\
**Created:** [September 30, 2022, 4:34pm UTC](https://discourse.julialang.org/t/how-do-you-preform-multiple-actions-in-a-thread-safe-way/88049 "2022-09-30T16:34:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [September 30, 2022, 4:34pm UTC](https://discourse.julialang.org/t/how-do-you-preform-multiple-actions-in-a-thread-safe-way/88049/1 "2022-09-30T16:34:49Z")

</div>

I need to check the sums of different combinations from a matrix, and get the biggest sum out. There are `20!` combinations. I need to thread safely check if the loop’s current sum is bigger than the current sum and if it is, assign both the sum and the index of the corresponding combination.

If I understand correctly, if this is done like this:

```julia
using Combinatorics, LinearAlgebra
asize = 5
A = rand(UInt32, asize ,asize )
mutable struct AtomSum
     @atomic sum::Int
     @atomic ind::Matrix{Bool}
end
currmax = AtomSum(0,[0 0; 0 0])

Threads.@threads for i in collect(permutations(1:asize))
    index = I(asize)[i,:]
    if (su = sum(A[index])) > currmax.sum
        @atomic currmax.sum = su
        @atomic currmax.ind = index
    end
end

```

Then lines 12-14 can be executed discontinuously, thus possibly assigning the wrong index to a sum that isn’t necessarily bigger than the previous?

How can I do all three in one thread safe operation?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 30, 2022, 5:19pm UTC](https://discourse.julialang.org/t/how-do-you-preform-multiple-actions-in-a-thread-safe-way/88049/2 "2022-09-30T17:19:00Z")

</div>

Probably the best you can do there is to create independent copies of all data for each thread, and at the end of your trials keep the best result. Using atomic operations there will cause the threaded operations to pause frequently, probably, and by copying the matrix for each thread you avoid the concurrent access to the memory by each thread.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 30, 2022, 5:50pm UTC](https://discourse.julialang.org/t/how-do-you-preform-multiple-actions-in-a-thread-safe-way/88049/3 "2022-09-30T17:50:31Z")

</div>

Use a channel and have each thread write a tuple to the channel, then have an extra thread process those tuples…
