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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:36:35+00:00 2026-06-18T23:36:35+00:00

This pattern seems to be working for me to achieve thread locking in this

  • 0

This pattern seems to be working for me to achieve thread locking in this thread unsafe environment.

However in terms of patterns and best practice (especially as I have figured it out myself), I’m not overly mad on exposing two collections that collate the same results. But the unsafe collection does need to be exposed publically and I don’t want to make it private and have an ‘AddResult(x)’ method.

Is this the correct way to solve this problem?

public class UnsafeObject
{
    public ObservableCollection<HighSpeedObject> ResultsUnsafe { get; set; }

    /// Accessed by UI thread once every 100ms
    public List<HighSpeedObject> Results
    {
        get
        {
            lock (_padlock)
            {
                return ResultsUnsafe.ToList();
            }
        }
    }

    private readonly static object _padlock = new object();
}
  • 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-18T23:36:36+00:00Added an answer on June 18, 2026 at 11:36 pm

    The instance of a ObservableCollectionz<T> class is not thread safe, so your solution is not stable.

    The lock in the Results property only ensures that one thread at a time can use that property, but it doesn’t protect the ResultsUnsafe property. The other threads can change the collection in the ResultsUnsafe property while the Results property is creating a list from it.


    Side note: You are using a static member as identifier for a lock for non-static data. That means that the lock will prevent access in all instances of the class, not just the instance where the data is that you want to protect. To protect static data you should use a static member as identifier, and to protect instance data you should use an instance member as identifier.

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

Sidebar

Related Questions

I'm implementing the specification pattern. The NotSpecification seems simple at first: NotSpecification.IsSpecialCaseOf(otherSpecification) return !this.specification.isSpecialCaseOf(otherSpecification)
The repository pattern seems to work well when working with an initial project with
I've seen this pattern used quite a bit, but it seems IE9 doesn't like
I know about the dependency injection pattern which seems like the best solution available
I've tried to accomplish this with regex but it seems not to be working
This pattern pops up a lot. It looks like a very verbose way to
So this pattern: def foo(&block) block.call end foo lambda { puts 'hi' } Is
Kept on seeing this pattern in code, but couldn't find any reference to it
What does this pattern '/\\\(?!&amp;#|\?#)/' match in PHP preg_replace function? Is this pattern valid?
My URLconf contains this pattern: url(r'^accounts/logout/$','django.contrib.auth.views.logout', name=logout), And I've trying to reverse that in

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.