as74
as74

Reputation: 710

Proper use of ParallelOptions, TaskCreationOptions and Task.Factory.StartNew?

Please suggest which method is proper(if any) for correct and efficient use of ParallelOptions, TaskCreationOptions and Task.Factory.StartNew(() =>.

private void NeedToUse_MaxDegreeOfParallelism_Method1()
{
    CancellationTokenSource tokenFor_task = new CancellationTokenSource();

    ParallelOptions parOpts = new ParallelOptions();
    //parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token;
    parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
    //parOpts.TaskScheduler = TaskScheduler.Default;

    TaskCreationOptions tco = new TaskCreationOptions();
    tco = TaskCreationOptions.PreferFairness;

    Task task = null;
    task = Task.Factory.StartNew(() =>
    {
        while (!tokenFor_task.IsCancellationRequested)
        {
            LongRunningMethod();
        }
    }, tokenFor_task.Token, tco, TaskScheduler.Default);
}


private void NeedToUse_MaxDegreeOfParallelism_Method2()
{
    //CancellationTokenSource tokenFor_task = new CancellationTokenSource();

    ParallelOptions parOpts = new ParallelOptions();
    parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token;
    parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
    parOpts.TaskScheduler = TaskScheduler.Default;

    TaskCreationOptions tco = new TaskCreationOptions();
    tco = TaskCreationOptions.PreferFairness;

    Task task = null;
    task = Task.Factory.StartNew(() =>
    {
        while (!parOpts.CancellationToken.IsCancellationRequested)
        {
            LongRunningMethod();
        }
    }, parOpts.CancellationToken, tco, parOpts.TaskScheduler);
}

private void NeedToUse_MaxDegreeOfParallelism_Method3()
{
    CancellationTokenSource tokenFor_task = new CancellationTokenSource();

    ParallelOptions parOpts = new ParallelOptions();
    //parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token;
    parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
    //parOpts.TaskScheduler = TaskScheduler.Default;

    TaskCreationOptions tco = new TaskCreationOptions();
    tco = TaskCreationOptions.PreferFairness;

    Task task = null;
    task = Task.Factory.StartNew(() =>
    {
         Parallel.Invoke(parOpts, () =>
        //while is already in LongRunningMethod() because can not be here
        //while (!tokenFor_task.IsCancellationRequested) 
        //{
            LongRunningMethod()
        //}
        );
    }, tokenFor_task.Token, tco, TaskScheduler.Default);
}

private void NeedToUse_MaxDegreeOfParallelism_Method4()
{
    CancellationTokenSource tokenFor_task = new CancellationTokenSource();

    ParallelOptions parOpts = new ParallelOptions();
    //parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token;
    parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount;
    //parOpts.TaskScheduler = TaskScheduler.Default;

    TaskCreationOptions tco = new TaskCreationOptions();
    tco = TaskCreationOptions.PreferFairness;

    Task task = null;
    Parallel.Invoke(parOpts, () =>
        task = Task.Factory.StartNew(() =>
        {
            while (!tokenFor_task.IsCancellationRequested)
            {
                LongRunningMethod();
            }
        }, tokenFor_task.Token, tco, TaskScheduler.Default)
    );
}

Currently I don't get any errors. First and second methods do not take into account MaxDegreeOfParallelism which I need to use. Ideally I would not use Parallel.Invoke but how to include parOpts.MaxDegreeOfParallelism in Task.Factory.StartNew?

Upvotes: 4

Views: 8000

Answers (1)

svick
svick

Reputation: 245076

Your code and question don't make much sense. Task.Factory.StartNew() doesn't accept MaxDegreeOfParallelism, because it executes a single action. Parallel.Invoke() does accept that parameter, but it doesn't make any sense to use that method when you have a single action.

Instead of asking a very specific question like this, I think you should step back, look at what you're actually trying to achieve and then possibly ask a new question about that.

EDIT: Now I think I finally understand what you're trying to do: on each core, you want to execute a separate loop. To do that, you could for example use Parallel.For():

Parallel.For(0, Environment.ProcessorCount, parOpts, () =>
    {
        while (!tokenFor_task.IsCancellationRequested)
        {
            LongRunningMethod();
        }
    });

Upvotes: 6

Related Questions