Skip to content

Instantly share code, notes, and snippets.

@oak-tree
Created August 16, 2022 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oak-tree/54dc24b910c728156695d1efc16be28e to your computer and use it in GitHub Desktop.
Save oak-tree/54dc24b910c728156695d1efc16be28e to your computer and use it in GitHub Desktop.
realtime
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, &params);
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, &params);
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