| C# Threading QA-21-30 | C# Question Answers-121 - 130 | |
Dot net Framework (C#) Threading Question Answers - 31-40 |
A Barrier is an object that prevents individual tasks in a parallel operation from continuing until all tasks reach the barrier. It allows multiple threads to cyclically synchronize at a point before proceeding to the next phase.
A Barrier is an object that prevents individual tasks in a parallel operation from continuing until all tasks reach the barrier.
The Barrier provides a way to cyclically synchronize multiple threads so that they all block at the same point and wait for all other threads to complete. A barrier is useful when one or more threads require the results of another thread before continuing to the next phase of an algorithm.
A group of tasks cooperate by moving through a series of phases, where each in the group signals it has arrived at the Barrier in a given phase and implicitly waits for all others to arrive. The same Barrier can be used for multiple phases.
ConcurrentBag<T> is a thread-safe, unordered collection of objects. It accepts null for reference types and minimizes synchronization overhead.
Deadlock is a situation where two or more threads are blocked indefinitely, each waiting for the other to release a resource, causing circular wait and no progress.
Releases the writer lock and restores the reader lock that the thread held before upgrading. It does not block other threads while downgrading.
DowngradeFromWriterLock releases the writer lock, regardless of the recursive lock count, and restores the reader lock that was held by the thread before upgrading to the writer lock. The lock count on the reader lock is restored.
A thread does not block when downgrading from the writer lock, even if other threads are waiting for the writer lock, because all reader-lock requests are granted when the writer lock is released.
Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection.
A Finalize() method acts as a safeguard to clean up resources in the event that your Dispose method is not called. You should only implement a Finalize method to clean up unmanaged resources. You should not implement a Finalize method for managed objects, because the garbage collector cleans up managed resources automatically. By default, the Object.Finalize method does nothing. If you want the garbage collector to perform cleanup operations on your object before it reclaims the object's memory, you must override this method in your class.
This method overrides Finalize. Application code should not call this method; an object's Finalize method is automatically invoked during garbage collection, unless finalization by the garbage collector has been disabled by a call to the SuppressFinalize() method.
It should only be implemented for unmanaged cleanup. By default, Object.Finalize does nothing. Application code should not call it directly.
The Interlocked class provides atomic operations (e.g., increment, decrement, exchange) for synchronizing access to shared variables across threads.
The lock keyword calls Enter at the start of the block and Exit at the end of the block. This ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released. Example:
The lock keyword ensures mutual exclusion. Only one thread can enter the critical section:
lock (thisLock)
{
if (amount > balance)
throw new Exception("Insufficient funds");
balance -= amount;
}
ManualResetEvent lets threads signal one another. Once signaled, it remains signaled until manually reset. WaitOne returns immediately when it is signaled.
ManualResetEvent allows threads to communicate with each other by signaling. Typically, this communication concerns a task which one thread must complete before other threads can proceed.
Once it has been signaled, ManualResetEvent remains signaled until it is manually reset. That is, calls to WaitOne return immediately.
You can control the initial state of a ManualResetEvent by passing a Boolean value to the constructor, true if the initial state is signaled and false otherwise.
Monitor synchronizes access to code blocks using Enter, TryEnter, and Exit. It provides critical section management. Methods are static and work with an object lock.
Provides a mechanism that synchronizes access to objects.
The Monitor class allows you to synchronize access to a region of code by taking and releasing a lock on a particular object by calling the Monitor.Enter, Monitor.TryEnter, and Monitor.Exit methods. Object locks provide the ability to restrict access to a block of code, commonly called a critical section. While a thread owns the lock for an object, no other thread can acquire that lock. You can also use the Monitor class to ensure that no other thread is allowed to access a section of application code being executed by the lock owner, unless the other thread is executing the code using a different locked object.
Monitor has the following features:
MTA is a threading model used in COM (Component Object Model) programming, especially relevant in .NET when dealing with COM interop.
[MTAThread] to indicate this model.
[MTAThread]
static void Main() {
// Your code here
}
If you're working with COM components that expect MTA, this attribute ensures compatibility.
| C# Threading QA-21-30 | C# Question Answers-121 - 130 | |