ParallelOptions.MaxDegreeOfParallelism Property

Gets or sets the maximum degree of parallelism enabled by this ParallelOptions instance.

Namespace:  System.Threading.Tasks
Assembly:  mscorlib (in mscorlib.dll)

public int MaxDegreeOfParallelism { get; set; }

Property Value

Type: System.Int32
An integer that represents the maximum degree of parallelism.

ExceptionCondition
ArgumentOutOfRangeException

The exception that is thrown when this MaxDegreeOfParallelism is set to 0 or some value less than -1.

The MaxDegreeOfParallelism limits the number of concurrent operations run by Parallel method calls that are passed this ParallelOptions instance to the set value, if it is positive. If MaxDegreeOfParallelism is -1, then there is no limit placed on the number of concurrently running operations.

By default, For() and ForEach() will utilize however many threads the underlying scheduler provides, so changing MaxDegreeOfParallelism from the default only limits how many concurrent tasks will be used.

The setting is largely there to provide control for advanced usage. For example, if you know that a particular algorithm you're using won't scale beyond a certain number of cores, you can apply a MaxDegreeOfParallelism to not try to take advantage of more cores and avoid wasting cycles. Or if you were running multiple algorithms concurrently and wanted to manually divide up how much of the system they could utilize, you could set a MaxDegreeOfParallelism for each.

It's also possible for certain workloads that the ThreadPool's heuristics will be unable to determine the right number of threads to utilize and could end up injecting many more than is appropriate... in such a case, you can set a MaxDegreeOfParallelism to ensure you don't use more than a reasonable number of threads. This behavior can be observed for really long running loop body iterations, where the ThreadPool can't tell the different between whether the iteration is making reasonable forward progress or whether it's livelocked or deadlocked, and can't reclaim threads it may have introduced to test whether more threads would help improve performance.

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft