# Atomic block in @async

**URL:** <https://discourse.julialang.org/t/atomic-block-in-async/7076>\
**Category:** Julia at Scale\
**Created:** [November 14, 2017, 11:12pm UTC](https://discourse.julialang.org/t/atomic-block-in-async/7076 "2017-11-14T23:12:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [November 14, 2017, 11:12pm UTC](https://discourse.julialang.org/t/atomic-block-in-async/7076/1 "2017-11-14T23:12:33Z")

</div>

I am using my own version of pmap as described here:  
[https://docs.julialang.org/en/stable/manual/parallel-computing#Synchronization-With-Remote-References-1](https://docs.julialang.org/en/stable/manual/parallel-computing#Synchronization-With-Remote-References-1)

Is there a way to run some block atomically?  
I have an array where I push data to and delete data.  
At the moment I am using `findmax()` and after that `deleteat` where the index depends on `findmax` is there a way to run these atomically?  
Looks like sometimes a push changes the index and then the wrong element gets deleted.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [November 15, 2017, 1:48am UTC](https://discourse.julialang.org/t/atomic-block-in-async/7076/2 "2017-11-15T01:48:36Z")

</div>

I can’t comment on your particular application because you haven’t given enough information.

But note this from the section of the documentation that you referenced:

> [@](#):
>
> No locking is required, since the threads are scheduled cooperatively and not preemptively. This means context switches only occur at well-defined points: in this case, when remotecall\_fetch() is called.

If your `findmax` and subsequent `deleteat` is in the `@async` block on the local process, then it should be atomic since task switching is cooperative.

On the other hand, it might not be atomic if for example your `findmax` and `deleteat` are separated by a blocking operation (e.g. `remotecall_fetch`), or if `findmax` is calculated and returned remotely on a SharedArray.

---

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [November 15, 2017, 2:02am UTC](https://discourse.julialang.org/t/atomic-block-in-async/7076/3 "2017-11-15T02:02:04Z")

</div>

I’m still trying to figure out where exactly the issue is. Everything I’m interested in atm is the master processor. That one works on normal arrays. I’ll add a comment when I figure out what exactly is wrong if it has something to do with this. Thanks 😉
