How to check that n methods are executed in parallel?

For knowledge purposes only, how can I verify that n methods are executed in parallel?

Parallel.For(0, 10, i => DoSomething(i));

...

void DoSomething(int par)
{
    Console.WriteLine($"Test: {par}");
}

Is there a test that can assure me that DoSomething is not executed sequentially (if Console.WriteLinenot a good test, what could be a very simple test function?)

+4
source share
3 answers

In your example, the methods will be definitely sequential, since the .NET console will synchronize calls from different threads .

, , Thread.ManagedThreadID Environment.CurrentManagedThreadId ( .NET 4.5)

class Program
{
    static void Main(string[] args)
    {
        // first run with default number of threads
        Parallel.For(0, 10, i => DoSomething(i));
        Console.ReadLine();

        // now run with fewer threads...
        Parallel.For(0, 10, new ParallelOptions{ 
                                  MaxDegreeOfParallelism = 2 
                                }, i => DoSomething(i));
        Console.ReadLine();
    }

    static void DoSomething(int par)
    {
        int i = Environment.CurrentManagedThreadId;
        // int i = Thread.ManagedThreadId;  -- .NET 4.0 and under
        Thread.Sleep(200);
        Console.WriteLine("Test: "+ par.ToString() + 
                          ", Thread :" + i.ToString());
    }
}

Thread.Sleep, , , , , , .

( ) , , .

, , , . , , , , . , (, ) .

, , - , , . parallelism, .

+5

, :

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ParallelMethodsCounter
{
    class Program
    {
        public static int _parallelCounter = 0;
        public static int _maxParallelExecutionsCount = 0;

        static void Main(string[] args)
        {
            Parallel.For(0, 10, i => DoSomething(i));
            Console.WriteLine("MAX PARALLEL EXECUTIONS: {0}", _maxParallelExecutionsCount);
        }

        public static void DoSomething(int par)
        {
            int currentParallelExecutionsCount = Interlocked.Increment(ref _parallelCounter);
            InterlockedExchangeIfGreaterThan(ref _maxParallelExecutionsCount, currentParallelExecutionsCount, currentParallelExecutionsCount);
            Console.WriteLine("Current parallel executions: {0}", currentParallelExecutionsCount);
            try
            {
                //Do your work here
            }
            finally
            {
                Interlocked.Decrement(ref _parallelCounter);
            }
        }

        public static bool InterlockedExchangeIfGreaterThan(ref int location, int comparison, int newValue)
        {
            int initialValue;
            do
            {
                initialValue = location;
                if (initialValue >= comparison) return false;
            }
            while (Interlocked.CompareExchange(ref location, newValue, initialValue) != initialValue);
            return true;
        }
    }
}

, Parallel.For , , . 2-4 5 (, , ).

+1

Not sure if I understand what you want to do. You can just count them.

Parallel.For(0, 10, i => DoSomething(i));

...

private static int parallelCounter = 0;
private static object counterLockObject = new Object();
void DoSomething(int par)
{
    int myCounter;
    lock(counterLockObject)
    {
        myCounter = ++parallelCounter;
    }
    try
    {
        // you probably need something that takes more time to
        // see anything executing in parallel
        Console.WriteLine("Current number of parallel threads: {0}", myCounter);
    }
    finally
    {
        lock(counterLockObject)
        {
            parallelCounter--;
        }        
    }
}
0
source

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


All Articles