LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-21-2010, 02:35 PM   #1
longp
LQ Newbie
 
Registered: Jul 2005
Location: canada
Posts: 5

Rep: Reputation: 0
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
 
Old 02-21-2010, 10:49 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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.

Last edited by neonsignal; 02-21-2010 at 10:53 PM.
 
Old 02-22-2010, 07:47 AM   #3
longp
LQ Newbie
 
Registered: Jul 2005
Location: canada
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 02-22-2010, 12:56 PM   #4
longp
LQ Newbie
 
Registered: Jul 2005
Location: canada
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 02-22-2010, 04:38 PM   #5
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What's the priority of process that contain multi-thread? aixilin Programming 5 10-04-2012 02:27 PM
need to find the appropriate thread scheduling and priority saurin Programming 1 09-10-2009 04:23 AM
priority for kernel thread? VGM Programming 0 04-15-2009 11:31 PM
Regarding giving the priority to a thread navderm Programming 2 12-28-2008 11:32 PM
thread priority tornatom Programming 0 08-22-2005 01:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration