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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:02:16+00:00 2026-05-13T13:02:16+00:00

I have a class with several data members: public class Foo { List<int> intLst;

  • 0

I have a class with several data members:

public class Foo
{
    List<int> intLst;
    int Whatever;
    HashTable<string> hshTable;
    ...
}

I then have a function within Foo that takes two Foos and merges them into a new Foo. I would like to make the Foo class thread safe too. Is the below code the best way to do this?

public Foo(Foo f)
{
    lock(f)
    {
        foreach (int i in f.intLst)
        {
            this.intLst.Add(i)
        }
        //More complicated
    }
}

public Foo Merge(Foo f1, Foo f2)
{
    Foo fReturn = new Foo(f1);//Using the constructor above

    lock(f2)
    {
        foreach (int i in f2.intLst)
        {
            fReturn.intLst.Add(i);
        }

        //More complicated merge code
    }

    return fReturn;
} 

Is it that simple? Should I use Monitors instead? I know lock implements the monitor class, but is there perhaps still a reason. Also do I need to worry about handling deadlock in my above sample.

I feel like a I need to lock() both f1 and f2 because what if f2 changes during the copy constructor on f1?

  • 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-13T13:02:16+00:00Added an answer on May 13, 2026 at 1:02 pm

    Yes, it is that simple.

    Your code is good. You don’t need to lock f2 until you access it, and you don’t need to lock f1 after it has been copied. So there really is no need to lock both at the same time.

    In your example the items themselves are simple integers which are immutable so there is no need to lock them, but if you had mutable items you may need to lock the individual items.

    You don’t need to worry about deadlock with just this code since you only hold one lock at a time. Of course if some other code calls it while holding a lock you might need to worry about the possiblity of deadlock.

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

Sidebar

Related Questions

Possible Duplicate: jquery data selector I have several elements with class 'connection_name'. Each one
I have one main class with several inherited members who all overload the same
I'm trying to write a class which contains several std::vectors as data members, and
I have the article class with several private vars. Then I'm trying to open
I have a Binary-Tree class template with several public and private member functions. When
I have a class that holds several vectors of objects: struct ComponentA { public:
I have a class containing several properties (all are strings if it makes any
I have a class that has several member classes as attributes. The constructor of
I have a class library containing several structures each consisting of several value and
I have a class that currently has several methods that take integer parameters. These

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.