3

I am sure this question would have been asked a lot of times, but I want to be clear of this concept.

Recently I came across a bug when two threads were accessing the same list in two different functions. In what scenarios can that cause problems if I don't have the proper locking? Even though I have a lock in the second function and the two threads are not related, they are manipulating the same list. One is adding and one is swapping with an empty list. Under what circumstances will it catch an exception? Please help.

PseudoCode:

List<SomeClass> list=new List<SomeClass>();

  object mutex=new object();

Thread 1 accesses this function and modifies the list:

public void Manipulate()
{

  //some operation
  list.add(new SomeClass());

}

Thread 2 access this function and clears the list:

public void SwapList()
{
   List<SomeClass> cls=new List<SomeClass>();
   try
   {
     while(Thread2.isAlive)
     {
       //some operation
       if(list.Count()>0)
       {
         lock(mutex)
         {
           swap(ref list,ref cls)
         }
       }
     }
  }
  catch(exception ex)
  {
  }
}
public void swap(List<SomeClass> a, List<SomeClass> b)
{
  List<SomeClass> temp=a;
  a=b;
  b=temp;
}
5
  • Doesn't answer your question but you should use a concurrent bag for multithreaded lists Commented Oct 18, 2013 at 23:46
  • do you mean "isn't", @svick? Commented Oct 19, 2013 at 0:22
  • @MillieSmith Right, yeah, typo. Commented Oct 19, 2013 at 0:26
  • “Under what circumstances will it catch an exception?” That's not the right question to ask, because an exception isn't the only way something could go wrong. Commented Oct 19, 2013 at 0:27
  • You are not experienced enough with multi-threading that you can get clever. Don't be clever. Commented Oct 19, 2013 at 12:04

2 Answers 2

5

In what scenarios can that cause problems if I don't have the proper locking?

Whenever you have at least two threads accessing the data at the same time and at least one of them is writing.

Because of that, you should lock whenever you have some data that can be accessed from multiple threads. And you need to lock every access to that data.

As an alternative, you could use thread-safe data structures (like collections in System.Collections.Concurrent). If you do that, you don't have to worry about locking yourself, because the structures already do it correctly (and efficiently).

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for System.Collections.Concurrent wich is dedicated to perform those locks : github.com/dotnet/runtime/blob/main/src/libraries/…
2

The locking should start as soon as you use the list:

lock(mutex)
{
  if(list.Count()>0)    
  {
     swap(ref list,ref cls)
   }
}

And it only helps if all those who wants to use it lock it (where is thread 1 locking?)

Edit:

Locking in thread 1 must be done in order for you to avoid race.

public void Manipulate()
{
  lock(mutex)
  {
    //some operation
    list.add(new SomeClass());
  }
}

I think reading about Thread-Safe Collections may help you - you can mostly avoid handle locking yourself in such cases.

About catching exceptions - in this specific case I can see no exception thrown from a case of concurrency. If you had a remove method, and you would try to swap specific items in your list it might have happened.

But there might be several other exceptions that might be happen, so I wouldn't put a block that catch any exception, but only those I can handle (swallowing exceptions is bad practice.)

3 Comments

The list manipulation in the Manipulate() method should also be locked, as Manipulate() could be called concurrently from different threads.
@elgonzo - Sure. That is what I meant in - "where is thread 1 locking?".
Thanks for all the explanation, but what I really meant was if you guys can describe a scenario where I can catch exception due to bad multithreding. I understand that locking would mean that my list is never corrupted but that won't bring down my application. So what can be the scenario where I can catch an exception, so let say inside catch block I have this code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.