Created
August 16, 2022 10:20
-
-
Save oak-tree/54dc24b910c728156695d1efc16be28e to your computer and use it in GitHub Desktop.
realtime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void SetRealTimePriority() { | |
int ret; | |
pthread_t this_thread = pthread_self(); | |
// used to store the scheduling priority | |
struct sched_param params; | |
// set the priority to the maximum. | |
params.sched_priority = sched_get_priority_max(SCHED_FIFO); | |
LOG(INFO) << "Trying to set thread realtime prio = " << params.sched_priority << std::endl; | |
// Attempt to set thread real-time priority to the SCHED_FIFO policy | |
ret = pthread_setschedparam(this_thread, SCHED_FIFO, ¶ms); | |
if (ret != 0) { | |
LOG(ERROR) << "Unsuccessful in setting thread realtime prio" << std::endl; | |
pthread_exit(NULL); | |
} | |
// verify the change in thread priority | |
int policy = 0; | |
ret = pthread_getschedparam(this_thread, &policy, ¶ms); | |
if (ret != 0) { | |
LOG(ERROR) << "Couldn't retrieve real-time scheduling paramers" << std::endl; | |
pthread_exit(NULL); | |
} | |
// Check the correct policy was applied | |
if (policy != SCHED_FIFO) { | |
LOG(ERROR) << "Scheduling is NOT SCHED_FIFO!" << std::endl; | |
} else { | |
LOG(INFO) << "SCHED_FIFO OK" << std::endl; | |
} | |
// Print thread scheduling priority | |
LOG(INFO) << "Thread priority is " << params.sched_priority << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment