Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:49:00+00:00 2026-06-12T23:49:00+00:00

When ranging over a map m that has concurrent writers, including ones that could

  • 0

When ranging over a map m that has concurrent writers, including ones that could delete from the map, is it not thread-safe to do this?:

for k, v := range m { ... }

I’m thinking to be thread-safe I need to prevent other possible writers from changing the value v while I’m reading it, and (when using a mutex and because locking is a separate step) verify that the key k is still in the map. For example:

for k := range m {
    m.mutex.RLock()
    v, found := m[k]
    m.mutex.RUnlock()
    if found {
        ... // process v
    }
}

(Assume that other writers are write-locking m before changing v.) Is there a better way?

Edit to add: I’m aware that maps aren’t thread-safe. However, they are thread-safe in one way, according to the Go spec at http://golang.org/ref/spec#For_statements (search for “If map entries that have not yet been reached are deleted during iteration”). This page indicates that code using range needn’t be concerned about other goroutines inserting into or deleting from the map. My question is, does this thread-safe-ness extend to v, such that I can get v for reading only using only for k, v := range m and no other thread-safe mechanism? I created some test code to try to force an app crash to prove that it doesn’t work, but even running blatantly thread-unsafe code (lots of goroutines furiously modifying the same map value with no locking mechanism in place) I couldn’t get Go to crash!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:49:01+00:00Added an answer on June 12, 2026 at 11:49 pm

    No, map operations are not atomic/thread-safe, as the commenter to your question pointed to the golang FAQ “Why are map operations not defined to be atomic?”.

    To secure your accessing it, you are encouraged to use Go’s channels as a means of resource access token. The channel is used to simply pass around a token. Anyone wanting to modify it will request so from the channel – blocking or non-blocking. When done with working with the map it passes the token back to the channel.

    Iterating over and working with the map should be sufficiently simple and short, so you should be ok using just one token for full access.

    If that is not the case, and you use the map for more complex stuff/a resource consumer needs more time with it, you may implement a reader- vs writer-access-token. So at any given time, only one writer can access the map, but when no writer is active the token is passed to any number of readers, who will not modify the map (thus they can read simultaneously).

    For an introduction to channels, see the Effective Go docs on channels.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a music blog that provides review scores ranging from 0.0 -
Our codebase contains code in multiple languages, ranging from Python over C# to MATLAB
I have a set of over 100 different probabilities ranging from 0.007379 all the
I have looked at various methods ranging from string.erase/ ispunct etc. and I cant
I have raw observations of 500 numeric values (ranging from 1 to 25000) in
My app needs to display a graphical representation of a value ranging from -1
I am developing an iOS app that must handle several stereo audio files (ranging
I have a huge database which holds pairs of numbers (A,B), each ranging from
Using AppleScript, how can I change the background color of cells ranging from A3
if($title =~ s/(\s|^|,|\/|;|\|)$replace(\s|$|,|\/|;|\|)//ig) $title can be a set of titles ranging from President, MD,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.