I have an application that run开发者_运维百科s 44*3=132 threads + main thread and each 3 threads represent a tcp client network. Freeing all this takes a lot of time on app finalization so I'm looking for a way to make this faster.
Only one way I know - setting the priority level. So each thread has priority level = normal. How should I set the priority level of the main thread to the highest possible level so it frees all other threads faster?
I'd say that you need to look at what your threads are doing, and ask them to do it in parallel. In my service app, which can have hundreds of threads (some with child threads), I have a loop on shutdown which is effectively:
for I := 0 to ThreadCount - 1 do
Threads[I].Terminate;
for I := 0 to ThreadCount - 1 do
Threads[I].WaitFor;
for I := 0 to ThreadCount - 1 do
Threads[I].Free;
The threads close themselves down in a timely manner on being terminated, but by asking them all to shut down first, and then waiting for them, it can all happen that much faster. And of course if the first thread is the last to close, then the middle loop there will be very fast as it will always only take as long as the slowest thread to close.
If your problem is not helped by this, then we need to see your code I think.
The way to change the priority of the main thread is to call SetThreadPriority
on it. For the handle, just use GetCurrentThread
— you don't need to open a real handle.
That probably won't have any effect on the problem you're trying to solve, but you didn't ask how to solve your problem; you asked how to adjust thread priority. If you want to know how to make your threads get freed more quickly, then next time, ask about that instead.
精彩评论