LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   change priority when creating new thread (https://www.linuxquestions.org/questions/programming-9/change-priority-when-creating-new-thread-790605/)

longp 02-21-2010 02:35 PM

change priority when creating new thread
 
Hi

I have created an error logger thread in my application using pthread_create(), I want this thread to have a lower priority than my application's priority so that the application would run properly. Is there an easy way to do this?
I know that we can change priority for thread created with policies FIFO or RR, since I don't set the policy for my application, I don't know what to set for my thread.

Regards

Long

neonsignal 02-21-2010 10:49 PM

Setting SCHED_FIFO or SCHED_RR policies is only appropriate if you were setting high scheduling priority levels (unusual in a user application). The default policy is SCHED_OTHER.

There is sometimes confusion between scheduling priority and the nice level. Although SCHED_OTHER threads are all run at scheduling priority 0, the scheduling algorithm takes into account the nice level of the thread in determining the allocation of cpu resources. Unlike the scheduling priority, the nice level is merely a guide to how aggressively the thread is prioritized.

What this means is that you don't use the attr to change the scheduling policy or priority, but instead use setpriority to change the nice level of the thread.

However, I would note that if your logging thread is actually causing the application to fail, then you have an problem in your thread logic. Even if you could force prioritization (and you can't for SCHED_OTHER threads), priorities are an error prone way to solve thread communication issues. The nice level is there purely to improve the performance or responsiveness of a thread.

longp 02-22-2010 07:47 AM

Thank you for your info; Actually, my logging thread does not cause the application to fail, it's just that sometime, for debugging purpose, I have to generate a lots of logs and I don't want this thread to overrun

Regards

Long

longp 02-22-2010 12:56 PM

I just look at the description for setpriority function, is it supposed to be used for process only? If I can use this function, can I pass the threadId parameter from pthread_create(pthread_t threadId, pthread_attr_t, void *, void*) to who parameter of setpriority(int which, int who, int prio)

Regards

neonsignal 02-22-2010 04:38 PM

With the setpriority call, the PRIO_PGRP sets the nice level for all the threads in the group. Setting the PRIO_PROCESS sets the nice level for the individual thread.

The frustrating part of the documentation is that the terminology is not consistent. Early Unix had no threads, only processes. Once threads were introduced, they were implemented as lightweight processes. Unfortunately this means that when the documentation uses the word 'process', it isn't always obvious whether it means the process as a whole, or the threads.


All times are GMT -5. The time now is 02:09 PM.