Romazes88
Romazes88

Reputation: 29

Multithreading and change Priority

I create 3 Threads in main with 3 different method.

Thread t1 = new Thread(FirstThread);
Thread t2 = new Thread(SecondThread);
Thread t3 = new Thread(ThirdThread);

I need measured time on every thread and search less of them.

The time is measured with Stopwatch. I didn't find another way(in c/c++ i know method GetThreadTimes).

static void FirstThread()
{
    Stopwatch stopwatch = Stopwatch.StartNew();
    try
    {
        Enumerable.Range(1, 10000).Select(x => x).Sum();
    }
    finally
    {
        stopwatch.Stop();
        Console.WriteLine(stopwatch.Elapsed);
    }
}

After (on task) Dynamic change Thread's priority and again start method example:

thr2.priority = ThreadPriority.Highest;

I don't understand as i could begun threads with changed priority if this is IMPOSSIBLE, because thread start 1 time.

Out on display time with normal and low priority all threads.

p.s. I think that Stopwatch compared variable cast to double or I don't see any other way...

double timeInSecondsPerN = Stopwatch1.Elapsed.TotalMilliseconds

The main question to look the work of threads with different priorities in the program runtime

Upvotes: 0

Views: 1058

Answers (1)

Baaleos
Baaleos

Reputation: 1825

Thread priority will not necessarily make a task faster or accomplish 'more work' If you set 3 threads to all be priority highest - then they will all be competing for CPU cycles at the same speed/rate, probably the same speed as if they were all thread priority BelowNormal.

All it means is that if your application gets to the point of being deadlocked and competing for CPU Cycles, it knows which thread to satisfy first.

This feature of .Net loses its benefits if you set all threads to the same high value. The feature only has value, if you use the Higher priority settings for threads that genuinely need to be satisfied before all others within your application.

Eg: Important computational tasks with low IO usage. (Encryption, Cryptocurrency, Hashing etc)

If your application is not getting to the point of being thread-locked or utilizing 100% of a cpu core, then the priority feature will never kick in.

The Microsoft website on Thread.Priority demonstrates the effect of priority well.

https://msdn.microsoft.com/en-us/library/system.threading.thread.priority(v=vs.110).aspx

using System;
using System.Threading;
using Timers = System.Timers;

class Test
{
    static void Main()
    {
        PriorityTest priorityTest = new PriorityTest();

        Thread thread1 = new Thread(priorityTest.ThreadMethod);
        thread1.Name = "ThreadOne";
        Thread thread2 = new Thread(priorityTest.ThreadMethod);
        thread2.Name = "ThreadTwo";
        thread2.Priority = ThreadPriority.BelowNormal;
        Thread thread3 = new Thread(priorityTest.ThreadMethod);
        thread3.Name = "ThreadThree";
        thread3.Priority = ThreadPriority.AboveNormal;

        thread1.Start();
        thread2.Start();
        thread3.Start();
        // Allow counting for 10 seconds.
        Thread.Sleep(10000);
        priorityTest.LoopSwitch = false;
    }
}

class PriorityTest
{
    static bool loopSwitch;
    [ThreadStatic] static long threadCount = 0;

    public PriorityTest()
    {
        loopSwitch = true;
    }

    public bool LoopSwitch
    {
        set{ loopSwitch = value; }
    }

    public void ThreadMethod()
    {
        while(loopSwitch)
        {
            threadCount++;
        }
        Console.WriteLine("{0,-11} with {1,11} priority " +
            "has a count = {2,13}", Thread.CurrentThread.Name, 
            Thread.CurrentThread.Priority.ToString(), 
            threadCount.ToString("N0")); 
    }
}
// The example displays output like the following:
//    ThreadOne   with      Normal priority has a count =   755,897,581
//    ThreadThree with AboveNormal priority has a count =   778,099,094
//    ThreadTwo   with BelowNormal priority has a count =     7,840,984

Upvotes: 1

Related Questions