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 7934715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:43:04+00:00 2026-06-03T21:43:04+00:00

I have a dictionary being updated by one thread, and in another thread I’d

  • 0

I have a dictionary being updated by one thread, and in another thread I’d like to iterate over its values. Normally I’d use a lock, but this code is very performance-critical, and I want to avoid that if at all possible.

A special feature of my case is that I don’t care about absolute correctness of the iterator; if it misses entries that were removed after iteration started, or picks up ones added afterwards, that’s fine. I only require that it doesn’t raise any sort of ‘dictionary size changed during iteration’ exception.

Given this relaxed constraint on correctness, is there an efficient way to iterate the dictionary without using a lock?

Note: I’m aware that keys() is threadsafe in Python 2.x, but since that behavior has changed in 3.x, I want to avoid it.

  • 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-03T21:43:05+00:00Added an answer on June 3, 2026 at 9:43 pm

    No personal experience with this, but I read this some time ago: http://www.python.org/dev/peps/pep-3106/

    These operations are thread-safe only to the extent that using them in a thread-unsafe way may cause an exception but will not cause corruption of the internal representation.

    As in Python 2.x, mutating a dict while iterating over it using an iterator has an undefined effect and will in most cases raise a RuntimeError exception. (This is similar to the guarantees made by the Java Collections Framework.)

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

Sidebar

Related Questions

I have dictionary, like Dictionary<string, bool> accValues = new Dictionary<string, bool>() And I want
Does VBA have dictionary structure? Like key<>value array?
I have a dictionary (keys are integers, values are float). I would now ask
I currently have a menu with subitems that is being stored in this dictionary
I have a dictionary like the follow: public Dictionary<int, SpawnList> spawnEntities = new Dictionary<int,
If I have a dictionary like so, Dictionary<int, string> roadNames = new Dictionary<int, string>();
Have Dictionary <Int64, byte> that gets used a lot. I mean in a loop
I have a dictionary of objects; they are all POCO objects that should be
I have a Dictionary called AvailableBoxes. I want to lookup a string in this
I have a dictionary of strings that i want the user to be able

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.