THREADING

What is the difference between thread and process?

A thread is a path of execution that run on CPU, a process is a collection of threads that share the same virtual memory. A process has at least one thread of execution, and a thread always run in a process context.

Note:- Its difficult to cover threading interview question in this small chapter. These questions can take only to a basic level. If you are attending interviews where people are looking for threading specialist, try to get deeper in to synchronization issues as that’s the important point they will stress.

How can you avoid deadlock in threading?

A good and careful planning can avoid deadlocks.There are so many ways Microsoft has provided by which you can reduce deadlocks example Monitor, Interlocked classes, Wait handles, Event raising from one thread to other thread, ThreadState property which you can poll and act accordingly etc.

What is ReaderWriter Locks ?

You may want to lock a resource only when data is being written and permit multiple clients to simultaneously read data when data is not being updated. The ReaderWriterLock class enforces exclusive access to a resource while a thread is modifying the resource, but it allows nonexclusive access when reading the resource. ReaderWriter locks are a useful alternative to exclusive locks that cause other threads to wait, even when those threads do not need to update data.

What is ManualResetEvent and AutoResetEvent ?

Threads that call one of the wait methods of a synchronization event must wait until another thread signals the event by calling the Set method. There are two synchronization event classes. Threads set the status of ManualResetEvent instances to signaled using the Set method. Threads set the status of ManualResetEvent instances to no signaled using the Reset method or when control returns to a waiting WaitOne call. Instances of the AutoResetEvent class can also be set to signaled using Set, but they automatically return to nonsignaled as soon as a waiting thread is notified that the event became signaled.

More >

What are wait handles ?

Twist :- What is a mutex object ?

Wait handles sends signals of a thread status from one thread to other thread. There are three kind of wait modes :-

?   WaitOne.

?   WaitAny. ?   WaitAll.

When a thread wants to release a Wait handle it can call Set method. You can use Mutex (mutually exclusive) objects to avail for the following modes. Mutex objects are synchronization objects that can only be owned by a single thread at a time. Threads request ownership of the mutex object when they require exclusive access to a resource. Because only one thread can own a mutex object More >

What is a monitor object?

Monitor objects are used to ensure that a block of code runs without being interrupted by code running on other threads. In other words, code in other threads cannot run until code in the synchronized code block has finished.

SyncLock and End SyncLock statements are provided in order to simplify access to monitor object.

What is use of Interlocked class ?

Interlocked class provides methods by which you can achieve following functionalities :- ?   Increment Values.

?   Decrement values.

?   Exchange values between variables. ?   Compare values from any thread. in a synchronization mode.

Example :- System.Threading.Interlocked.Increment(IntA)

How can we know a state of a thread?

“ThreadState” property can be used to get detail of a thread. Thread can have one or  a combination of status.System.Threading. Threadstate enumeration has all the values to detect a state of thread. Some sample states are Isrunning, IsAlive, suspended etc.

Can we use events with threading ? `

Yes, you can use events with thread; this is one of the techniques to synchronize one thread with other.

When working with shared data in threading how do you implement synchronization ?

There are certain somethings that you need to be careful with when using threads. If two threads (e.g. the main and any worker threads) try to access the same variable at the same time, you’ll have a problem. This can be very difficult to debug because they may not always do it at exactly the same time. To avoid the problem, you can lock a variable before accessing it. However, if  the two threads lock the same variable at the same time, you’ll have a deadlock problem.

SyncLock x

‘Do something with x End SyncLock