# Why Dictionary crashed on concurrency write?

**URL:** <https://forums.swift.org/t/why-dictionary-crashed-on-concurrency-write/59242>\
**Category:** Using Swift\
**Tags:** concurrency, thread\
**Created:** [July 27, 2022, 5:01am UTC](https://forums.swift.org/t/why-dictionary-crashed-on-concurrency-write/59242 "2022-07-27T05:01:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DarkZhao](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@DarkZhao](https://forums.swift.org/u/DarkZhao)\
**Post date:** [July 27, 2022, 5:01am UTC](https://forums.swift.org/t/why-dictionary-crashed-on-concurrency-write/59242/1 "2022-07-27T05:01:14Z")

</div>

Here is the code:

```swift
var dict = [Int: Int]()
for i in 0...100 {
    DispatchQueue.global().async {
        dict[i] = i
    }
}

```

And I got this crash:

 ![image](https://global.discourse-cdn.com/swift/original/3X/c/8/c8840b30ff211f9104e114b7d2d68a763b64c823.png)

Xcode: 13.1  
Swift: 5.0+  
Mac: 12.1 M1

plz, it confuse me a lot

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [July 27, 2022, 5:14am UTC](https://forums.swift.org/t/why-dictionary-crashed-on-concurrency-write/59242/2 "2022-07-27T05:14:44Z")

</div>

As a general rule in Swift, you must not access the same variable (or property, or so on) multiple times concurrently unless all the accesses are reads. Assignments to elements of a dictionary are considered to be modifications of the dictionary as a whole, so your code is in violation of that.

It is a goal of the concurrency feature that we've been working on to diagnose this problem at compile time. If you enable the strict concurrency checking in Swift 5.7, you should already get a diagnostic for this, at least if the dictionary is a local variable; global variables, including top-level variables in playgrounds, will not necessarily get the same diagnostic.

Swift does not currently provide a concurrent dictionary that allows different keys to be safely modified from multiple threads at once.

---

<div class="post-metadata">

**Author:** ![DarkZhao](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@DarkZhao](https://forums.swift.org/u/DarkZhao)\
**Post date:** [July 27, 2022, 5:24am UTC](https://forums.swift.org/t/why-dictionary-crashed-on-concurrency-write/59242/3 "2022-07-27T05:24:51Z")

</div>

Thanks a lot! I understand it. Thanks！

---

<div class="post-metadata">

**Author:** ![DarkZhao](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@DarkZhao](https://forums.swift.org/u/DarkZhao)\
**Post date:** [July 27, 2022, 5:38am UTC](https://forums.swift.org/t/why-dictionary-crashed-on-concurrency-write/59242/4 "2022-07-27T05:38:17Z")

</div>

Another question: Does an `Int` as a key store on heap or stack?

---

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [July 27, 2022, 8:36pm UTC](https://forums.swift.org/t/why-dictionary-crashed-on-concurrency-write/59242/5 "2022-07-27T20:36:31Z")

</div>

> [@DarkZhao](#):
>
> Another question: Does an `Int` as a key store on heap or stack?

It’s not really “on the stack vs heap” so much as “with or without indirection”. That is, if you have an Int-keyed Dictionary (or an Array of Int) in Swift, the Int can be stored directly inline in the Dictionary’s storage, unlike in languages like Java or Objective-C where the collection just holds pointers, and those pointers point to the actual objects elsewhere on the heap.

(The dictionary or array storage itself though will normally be on the heap… but as a contiguous block of memory)

---

<div class="post-metadata">

**Author:** ![DarkZhao](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@DarkZhao](https://forums.swift.org/u/DarkZhao)\
**Post date:** [July 28, 2022, 12:21pm UTC](https://forums.swift.org/t/why-dictionary-crashed-on-concurrency-write/59242/6 "2022-07-28T12:21:28Z")

</div>

Thanks for you kind help.
