1

I am trying to do some multithreads calculations but I'm having some trouble to manage each thread independently.

For instance, if one thread finishes its calculation faster than the others, how can I detect it and start a new thread for the next calculation without waiting for the whole process and all the other thread to complete their own task ?

I am using C# .net 4.5 by the way.

Thanks

1
  • can you post some sample code? Commented Oct 11, 2013 at 11:37

2 Answers 2

1

Since you are using .NET4.5, why not just use the TPL - Task Parallel Library ? It does all that you are trying to do (task scheduling and management) and more.

http://msdn.microsoft.com/en-us/library/dd460717.aspx

From the site:

The Task Parallel Library (TPL) is a set of public types and APIs in the System.Threading and System.Threading.Tasks namespaces in the .NET Framework 4. The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications. The TPL scales the degree of concurrency dynamically to most efficiently use all the processors that are available. In addition, the TPL handles the partitioning of the work, the scheduling of threads on the ThreadPool, cancellation support, state management, and other low-level details. By using TPL, you can maximize the performance of your code while focusing on the work that your program is designed to accomplish.

Sign up to request clarification or add additional context in comments.

4 Comments

Mmm I already use TPL but I start a predefined number of tasks in a loop and wait for those tasks to finish before to continue the loop and start new one. But I want to continue with a new task when one finish without to wait others.
Just create all your Task<T> at in your loop, save the task objects in an array then use Task.WaitAll() and let TPL handle the job scheduling for you. Is this a Map/Reduce pattern? Or do you need to specify the sequence of task execution yourself?
It's actually what I do. I start predefined number of thread with my loop and wait them all before to continue. But I need to continue when one of them ended.
If you want to continue when any one of the tasks finish, use Task.WaitAny(). If you want to continue when all of them are finished, use Task.WaitAll(). You didn't provide enough information on why you need to start a predefined number of threads, but I've had good performance with just creating a Task<T> for all the concurrent work I need to get done, starting all the Task<T> at once and letting the TPL handle it, not worrying about any thread scheduling at all and just waiting for the tasks to complete. This is a common pattern in concurrent programming, not just in the .NET TPL.
1

Create TPL Task for each calculation, and start them, internally each task will be queued and will execute when threads get available, thus you don't have to bother about thread management, it will be handled internally.

if you want to limit maximum parallel task to execute at a time you can use Parallel.Invoke method and set MaxDegreeOfParallelism e.g.

Parallel.Invoke(
new ParallelOptions() { MaxDegreeOfParallelism = 3 }, 
() => Calculation1(), 
() => Calculation2(),
() => Calculation3(),
);

Parallel.Invoke takes an array of parameters and perform action on it to run them all in parallel, at the same time you can set MaxDegreeOfParallelism to set maximum number of calculations to be run at a time.

e.g.

I suppose you have same method (Calculate) but multiple parameters (Parameters) as input.

Parallel.ForEach(Parameters,new ParallelOptions() { MaxDegreeOfParallelism = 3 }, parameter=> Calculate(parameter));

5 Comments

Thanks Imran. It seems interesting solution. How you would do to use Parallel.Invoke in a loop ?
I've enhanced answer to include loop in paallel.invoke
It seems, after some tests, it doesn't work because number of executed thread is less than MaxDegreeOfParallelism. Do you know why and how to solve this ?
if this happens , it will wait for threads to get available and execute later. what more you want to do?
I would like to execute specific number of threads at the same time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.