Is threading priorities useless?

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
I have done several threading tests today, and so far it seems like setting priorities on threads is of no use

In short: I have a sub like this to start several threads (6 in total)

public sub StartThreads()
t1 = New Thread(AddressOf oSquare1.CalcSquare)
t1.Priority = ThreadPriority.Highest

t2 = New Thread(AddressOf oSquare2.CalcSquare)
t2.Priority = ThreadPriority.Lowest

t1.Start()
t2.Start()
End sub

The CalcSquare sub does a lot of useless calculations and now and then updates (through delegates) a progress bar on the form

What I have noticed so far:
- A thread will be between 90 and 100% done before the next thread starts (usually 100%), no matter how much I order it to calculate
- There's a 90% chance that the first thread started will be done first, completely ignoring priority


Here are some result from the last test I made:
Finished - Priority - Thread #
1st - Below normal - 1
2nd - highest - 2
3rd - normal - 3
4th - normal - 6
5th - above normal - 5
6th - highest - 4

Each calculation takes 12 seconds
If the above had just been a random coincidence it would have been ok, but the above test shows pretty much what I have been getting all the time. Or actually it usually is worse, this was one of the rare times where I saw "highest" be among the first two finished

Why is the set priority completely ignored?
 
The code example for Thread.Priority Property very much indicates the ThreadPriority is not useless. Try it and see if it makes sense to you.
 
Tried the code on that page, but no, it doesn't make sense why a thread with priority lowest gets a higher score than one at normal? But at least with this test those with highest scored highest

I created 7 threads with different priorities and got these results:

1st - highest
2nd - highest
3rd - lowest
4th - normal
5th - below normal
6th - above normal
7th - lowest

Those with highest scored a lot higher than the others though. But why doesn't it work like this in my own test?
The only differences between this test and my test are:
- Each thread has it's own object, not like the other test where all threads uses the same object
- Each thread reports through delegates to the form (updates a progress bar)

Edit: I might have found the problem, the code I used in my test also had some code I haven't noticed before. This code looks like it messes up threads

Edit 2: Seems like that did the trick, now priorities are considered. But there doesn't seem to be that much difference between the priorities, in my test "Highest" was only about 25% faster than "Lowest", had hoped for a bigger difference
 
Last edited:
Tried the code on that page, but no, it doesn't make sense why a thread with priority lowest gets a higher score than one at normal?
When I run that code I get significant difference between whatever thread is set lower and higher priority, and it doesn't matter which one is started first. There is also no difference if they use the same class instance for the threaded method or not. What I see is that threading priorities matters and is considered correctly due to the setting.
 
Back
Top