C # multi-threading - which method to use?

My goal is to write a program that processes an arbitrary number of tasks based on user input.

Suppose that in this case the number of tasks is 1000.

Now I would like to have a dynamic number of threads that spawn and run tasks one at a time.

I would suggest that I need to use the "synchronous" method, as opposed to the "asynchronous" method, so if one task has a problem, I would not want it to slow down the completion of the rest.

What method would I use to accomplish the above? Semaphores? ThreadPools? And how can I make sure that the thread is not trying to start a task that is already being processed by another thread? Will “blocking” handle this?

Examples of code and / or links to sites that point to me in the right direction will be appreciated.

edit: The problem with the Fibonacci MSDN example is that the waitall method can handle up to 64 expectations. I need more than 1000 tasks. How to fix this situation without creating deadlocks?

+3
source share
6 answers

Are these tasks independent? If so, you basically want a producer / consumer queue or a user stream that actually have different views on the same thing. You should be able to place tasks in a queue and have multiple threads to read from this queue.

MiscUtil ( - ) / ( ).

, threadpool - , , , . - .NET 4.0, Parallel Extensions, .

WaitAll... , ? / , , - "" (, , "quit" ), "WaitUntilEmpty" ( ). : , , ... , , .

+10

, ThreadPool .

MSDN , ThreadPool #. , .

Threading # .

- VS 2010 .NET 4, Task Parallel Library - - .

+2

(), Task .NET 4 . , ThreadPool - . () . : 1000 , , , Out Of Memory. ThreadPool .

, , (1) . ThreadPool .

+1
0
0

As already mentioned, .NET 4 has an excellent parallel task library. But you can use June 2008 CTP in .NET 3.5 just fine. I did this for some hobby projects myself, but if it is a commercial project, you should check if there are legal problems.

0
source

Source: https://habr.com/ru/post/1718128/


All Articles