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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:48:38+00:00 2026-05-28T03:48:38+00:00

I have an instance of a class that can be accessed by multiple threads.

  • 0

I have an instance of a class that can be accessed by multiple threads.

Inside this class is a [ThreadStatic] variable which stores various objects.

I now need a second instance of my class, and I want it to have a separate object store inside it.

Currently, two instances in the same thread will share the same object store. I do not want this.

The only solution I can think of is:

Have a static IDictionary<int, TObjectStore> where the int is the thread id, and access this via some method or getter:

static TObjectStore ObjectStore {
    get {
        // create the instance here if its the first-access from this thread, with locking etc. for safety
        return objectStore[Thread.CurrentThread.Id];
    }
}

The problem with this though is how do I dispose of the TObjectStore for a particular thread when it ends? I think im right in assuming that with my current implementation, the GC will simply pick it up?

Thanks

  • 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-05-28T03:48:38+00:00Added an answer on May 28, 2026 at 3:48 am

    A static field isn’t really in any instance, so I assume you now need an instance field. In that case, you want a ThreadLocal<T>:

    ThreadLocal<SomeType> store = new ThreadLocal<SomeType>( () => {
        // initializer, used when a new thread accesses the value
        return ...
    });
    

    This store will be available for collection along with the instance, as will any contents (as long as they aren’t referenced anywhere else, obviously).

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

Sidebar

Related Questions

I have a singleton class, that has a map which can be accessed by
Lets say we have an object that can be accessed by multiple threads and
I have a class that I can have many instances of. Inside it creates
i have a problem calling multiple instance of a class that i have coded
I have a custom class that has as an instance variable of a coordinate:
I have a C# singleton class that multiple classes use. Is access through Instance
I have a class Page that creates an instance of DB , which is
I have a java class that is accessed by a lot of threads at
I have an instance of Class A that I want to refer to in
I have an instance of a UITableView, and a separate class that adheres to

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.