• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

C# Concurrency in a nutshell

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
In this article, I will briefly discuss the most important aspects of concurrency in C#.

Concurrency vs. Parallelism

A system is said to be concurrent if it can support two or more actions in progress at the same time. A system is said to be parallel if it can support two or more actions executing simultaneously. — The Art of Concurrency book
To make breakfast, one person can boil eggs, make tea, and toast bread at the same time. However, they cannot drive to the grocery store and come back simultaneously. To make breakfast and buy groceries in parallel, you need at least two people.

A computer with a single CPU and a single thread can perform multiple tasks by quickly switching between them. However, to work on multiple jobs simultaneously, multiple threads are required. Each thread acts as a separate worker, allowing the CPU to allocate resources and process data more efficiently. By utilizing multiple threads, a computer can perform jobs in parallel, resulting in faster and more efficient processing.

Concurrency in C


To achieve concurrency in C#, you need to understand operating systems, threads, and the C# Asynchronous Pattern.

Multithreading


I won’t dive into how to work with threads in this article because we rarely deal with threads in day-to-day code in modern C#. Most of the time, we use the latest C# asynchronous pattern, TAP.

I recommend an excellent series of articles on

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

:

Asynchronous Patterns


C# introduced three asynchronous patterns over its lifetime. First, in C# 1.0, there was the Asynchronous Programming Model (APM) pattern.

C# 2.0 introduced the Event-based Asynchronous Pattern (EAP).

Finally, in C# 5.0, we got the latest and greatest of them all: the

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

, or TAP.

Each pattern simplifies asynchronous programming compared to its predecessors. For those interested in the details, I recommend reading this comprehensive article from the .NET blog; it’s worth your time:

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

.

TAP consists of the following key components:

  • The Task class — represents an asynchronous operation.
  • The async / await keywords — define asynchronous methods and wait for the completion of asynchronous operations.
  • Task-based APIs — a set of classes that work seamlessly with the Task class and async/await keywords.
What You Should Know About TAP

State Machine


When you add the “async” keyword to a method, the compiler creates a

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

machine in the background. This handles concurrency and frees you from worrying about thread management. However, creating a state machine can be expensive, as the compiler generates a lot of code behind the scenes. Therefore, it’s essential to avoid creating unnecessary state machines.

Here are some best practices to keep in mind:

Thread Synchronization


Thread synchronization in C# refers to the coordination and control of multiple threads to ensure their safe and orderly execution in a multi-threaded environment. It helps to prevent race conditions, data corruption, and other concurrency-related issues that can occur when multiple threads access shared resources concurrently.

Lock


In C#, the

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

keyword is used to ensure mutual exclusion or thread synchronization in a multi-threaded environment. When multiple threads access a shared resource concurrently, using the lock keyword helps prevent race conditions and ensures that only one thread can access the critical section at a time.

The lock keyword is used in conjunction with a synchronization object, typically an instance of an object. When a thread encounters a lock statement, it attempts to acquire the lock on the specified object. If the lock is already held by another thread, the current thread waits until the lock is released before proceeding.

Deadlock


A

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

is a situation where two or more threads are unable to proceed because each is waiting for a resource held by another thread in the deadlock group. This results in a circular dependency, causing all threads involved to be blocked indefinitely.

InterLocked


The

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

class provides atomic operations for variables shared among multiple threads. It offers thread-safe and atomic operations without the need for explicit locking mechanisms, such as lock or Monitor.

ReaderWriterLockSlim


The

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

class provides a synchronization mechanism that allows multiple threads to read a shared resource simultaneously while ensuring exclusive access for writing. It is a more efficient alternative to the older ReaderWriterLock class, reducing the overhead associated with acquiring and releasing locks by using lightweight synchronization primitives.

SemaphoreSlim


The

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

class provides a synchronization primitive that controls access to a limited number of resources among multiple threads. It is a lightweight alternative to the Semaphore class and is useful in scenarios where you need to limit concurrent access to a shared resource.

Thread Signaling


Thread signaling in C# refers to the mechanism of communication and coordination between multiple threads to ensure synchronized execution or to notify each other about specific events or conditions.

In multi-threaded scenarios, it is often necessary for threads to wait for certain conditions before proceeding or for one thread to notify another about a particular event. This is where thread signaling comes into play.

AutoResetEvent


The

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

class is a synchronization primitive that allows threads to wait until a signal is received and then automatically resets itself. It is a thread-signaling mechanism used for one-to-one thread coordination.

ManualResetEventSlim


The

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

class is a lightweight synchronization primitive that provides thread-signaling functionality similar to ManualResetEvent. It allows threads to wait until a signal is received and remains in the signaled state until manually reset.

CountdownEvent


The

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

class is a synchronization primitive that allows one or more threads to wait until a specified number of signals or operations have been completed. It provides a way to synchronize the execution of multiple threads and coordinate their completion.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу