Interview Question in .NET Threading

Could you describe two methods for synchronizing multithreaded recording on a class member?

Please can someone help me what this means and that is the correct answer.

+6
multithreading
source share
3 answers

When you change data in C #, something similar to one operation can be compiled into several instructions. Take the following class:

public class Number { private int a = 0; public void Add(int b) { a += b; } } 

When you create it, you get the following IL code:

 IL_0000: nop IL_0001: ldarg.0 IL_0002: dup // Pushes the value of the private variable 'a' onto the stack IL_0003: ldfld int32 Simple.Number::a // Pushes the value of the argument 'b' onto the stack IL_0008: ldarg.1 // Adds the top two values of the stack together IL_0009: add // Sets 'a' to the value on top of the stack IL_000a: stfld int32 Simple.Number::a IL_000f: ret 

Now, let's say you have a Number object, and two threads call its Add method as follows:

 number.Add(2); // Thread 1 number.Add(3); // Thread 2 

If you want the result to be 5 (0 + 2 + 3), a problem arises. You do not know when these threads will execute their instructions. Both threads could execute IL_0003 (pushing zero on the stack) before either IL_000a (actually changing the member variable), and you get the following:

 a = 0 + 2; // Thread 1 a = 0 + 3; // Thread 2 

The last thread to complete the "wins" and at the end of the process, a is 2 or 3 instead of 5.

So, you must make sure that one complete set of instructions ends before the other set. To do this, you can:

1) Block access to a member of the class while it is being written, using one of many . NET synchronization primitives (for example, lock , Mutex , ReaderWriterLockSlim , etc.), so that only one thread can work on it.

2) Move write operations to the queue and process this queue in a single thread. As Torarin points out, you still have to synchronize access to the queue if it is not thread safe, but it is worth it for complex write operations.

There are other methods. Some (for example, Interlocked ) are limited to specific data types, and there are even more (for example, those discussed in Non- Blocking Synchronization and Joseph Albahari Threading Part 4 in C # ), although they are more complex: use caution.

+13
source share

In multi-threaded applications, there are many situations where concurrent access to the same data can cause problems. In such cases, synchronization is required to ensure that only one thread has access at any given time.

I assume that they imply using lock -statement (or SyncLock in VB.NET) using Monitor .

You may want to read this page for examples and an understanding of the concept. However, if you have no experience developing multithreaded applications, this will most likely become apparent if your new employer takes you to a test. This is a pretty tricky question with many possible errors like deadlock .

There is a pretty good MSDN page on the subject .

There may be other parameters depending on the type of the member variable and how it is changed. For example, incrementing an integer can be done using Interlocked . Input method.

As an exercise and a demonstration of the problem, try writing an application that runs 5 simultaneous threads, increasing the total counter a million times per thread. The estimated end result of the counter will be 5 million, but this (maybe) is not what you get in the end :)

Edit: A quick implementation is done by itself ( download ). Output Example:

 Unsynchronized counter demo: expected counter = 5000000 actual counter = 4901600 Time taken (ms) = 67 Synchronized counter demo: expected counter = 5000000 actual counter = 5000000 Time taken (ms) = 287 
+12
source share

There are several ways, some of which are mentioned earlier.

  • ReaderWriterLockSlim is my preferred method. This gives you a type of database lock and allows you to update (although the syntax of this is incorrect on MSDN the last time I looked and is very unobvious). Lock statements
  • . You treat reading as writing and simply deny access to the variable
  • Blocking operations. This performs operations on the type of value in the atomic step. This can be used for streaming blocking (in fact, this is not recommended)
  • Mutexes and semaphores (did not use them)
  • Monitor statements (this essentially works with the lock keyword)

Although I do not want to deny the other answers, I will not trust anyone that does not use one of these methods. We apologize if I forget anything.

0
source share

All Articles