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

  • Home
  • SEARCH
  • 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 8032001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:15:58+00:00 2026-06-05T01:15:58+00:00

I have implemented a simple cache using the dictionary class: private Dictionary<int, byte[]> cache

  • 0

I have implemented a simple cache using the dictionary class:

private Dictionary<int, byte[]> cache = new Dictionary<int, byte>();

public void SetPicture(int id, byte[] bytes)
{
    cache[id] = bytes;
}

public byte[] GetPicture(int id)
{
    if (cache.Contains(id)) {
        return cache[id];
    }
    return null;
}

SetPicture is only called from a single background thread. (This background thread is updating user profile pictures from an active directory query).

GetPicture is called from multiple other threads (threads handling http requests).

Items will never be removed from the cache.

So is this code thread safe? Or do I need to block access to the internal Dictionary while writing to it in SetPicture?

  • 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-05T01:15:58+00:00Added an answer on June 5, 2026 at 1:15 am

    No it is not safe; all access to a dictionary needs to be synchronized, as the contents of a Dictionary<,> are not guaranteed for readers against a writer. Also, a separate contains/get check is an obvious thread-race.

    Options:

    • use a 1.1-style Hashtable – that supports one writer and multiple concurrent readers without synchronization (downside here is that your key, int, is a value-type, so will need to be boxed; Hashtable is more inviting when using a reference-type key, such as string)
    • use a ConcurrentDictionary<,>
    • use ReaderWriterLockSlim to synchronize (assuming reads are much more common than writes)
    • use lock to synchronize (assuming non-trivial writes)

    However! If you use the Hashtable approach do not do a separate Contains / get – just use the indexer. If you get null it wasn’t there. Otherwise you have a race condition.

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

Sidebar

Related Questions

I have the following class (from a simple Spring tutorial) public class CarValidator implements
I have a simple domain class Licensee: @Entity @Table(name = LICENSEE) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
I have implemented a simple linux shell in c. Now, I am adding some
I have implemented a simple RSS reader in Java. I hit a URL and
I am creating a simple messaging system in WCF and have implemented a minimal
Very simple Qt GUI application: On the scene I have multiple circles implemented as
i have this class called MemoryManager, it is supposed to implement a simple smart
I have implemented a simple parallel merge sort algorithm in Java. This cuts the
Am I using Session_End incorrectly? In my application, I have a cache to speed
I have a question regarding the Java Memory Model. Here is a simple class

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.