設置線程優先級的函數:dom
int pthread_setschedparam(pthread_t target_thread, int policy, const struct sched_param *param)函數
它主要用於設置線程的調用策略和優先級。線程
參數說明:指針
#ifdef HAVE_SCHED_H #include <sched.h> #endif void setCurrentThreadHighPriority(bool value) { // Start out with a standard, low-priority setup for the sched params. struct sched_param sp; bzero((void*)&sp, sizeof(sp)); int policy = SCHED_OTHER; // If desired, set up high-priority sched params structure. if (value) { // FIFO scheduler, ranked above default SCHED_OTHER queue policy = SCHED_FIFO; // The priority only compares us to other SCHED_FIFO threads, so we // just pick a random priority halfway between min & max. const int priority = (sched_get_priority_max(policy) + sched_get_priority_min(policy)) / 2; sp.sched_priority = priority; } // Actually set the sched params for the current thread. if (0 == pthread_setschedparam(pthread_self(), policy, &sp)) { printf("IO Thread #%d using high-priority scheduler!", pthread_self()); } }
描述來自:https://baike.baidu.com/item/pthread_setschedparam/7033773blog