#14 thread priority

closed
Davis
5
2006-12-08
2006-11-24
No

Is it possible to add some kind of priority to threads?
I know both linux and windows support setting the thread priority, so might be a good idea to add it (and I also need it ;)).

Greetz

Discussion

  • Davis

    Davis - 2006-11-24

    Logged In: YES
    user_id=1166290
    Originator: NO

    I considered adding this feature a few months ago but ended up not doing so. It doesn't seem like this is something that can be even remotely portable. For instance POSIX threads doesn't *really* supply any API to deal with setting a thread's priority. Sorta, it does give some things but their exact behavior is "implementation defined" so in effect it does not provide support.

    What are you trying to do specifically?

     
  • Steven Van Ingelgem

    Logged In: YES
    user_id=569271
    Originator: YES

    At the moment I am creating some program which needs to calculate on my computer for like 66 seconds before all possible routings have been found (and there is not a really less cpu consuming way to do it). So I thought like 'put a mutex & lock it till initial calculations are done', put this initial calculations inside a low priority thread. Because the user doesn't do anything (most of the time for like 2 minutes before the program is being used because then the game is being started). But I can't do it in a normal thread because that would consume all the possible cpu power...

    windows: SetThreadPriority
    linux: (thanks to wxWidgets: threadpsx.cpp @ 1237)
    #ifdef HAVE_THREAD_PRIORITY_FUNCTIONS
    #if defined(__LINUX__)
    // On Linux, pthread_setschedparam with SCHED_OTHER does not allow
    // a priority other than 0. Instead, we use the BSD setpriority
    // which alllows us to set a 'nice' value between 20 to -20. Only
    // super user can set a value less than zero (more negative yields
    // higher priority). setpriority set the static priority of a
    // process, but this is OK since Linux is configured as a thread
    // per process.
    //
    // FIXME this is not true for 2.6!!

    // map wx priorites WXTHREAD_MIN_PRIORITY..WXTHREAD_MAX_PRIORITY
    // to Unix priorities 20..-20
    if ( setpriority(PRIO_PROCESS, 0, -(2*prio)/5 + 20) == -1 )
    {
    wxLogError(_("Failed to set thread priority %d."), prio);
    }
    #else // __LINUX__
    {
    struct sched_param sparam;
    sparam.sched_priority = prio;

    if ( pthread_setschedparam(m_internal->GetId(),
    SCHED_OTHER, &sparam) != 0 )
    {
    wxLogError(_("Failed to set thread priority %d."), prio);
    }
    }
    #endif // __LINUX__
    #endif // HAVE_THREAD_PRIORITY_FUNCTIONS

    Best to check first if the thread is running...

     
  • Davis

    Davis - 2006-11-25

    Logged In: YES
    user_id=1166290
    Originator: NO

    Have you tried doing this with all your threads being normal priority? I would expect that it would work just fine. The OS should schedule the threads in a way that prevents thread starvation.

     
  • Davis

    Davis - 2006-12-08
    • status: open --> closed
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks