#include <iostream>
#include <pthread.h>
void* threadFunction(void* arg) {
// 线程函数逻辑
// ...
return nullptr;
}
int main() {
pthread_t thread;
pthread_attr_t attr;
// 初始化线程属性
pthread_attr_init(&attr);
// 设置线程优先级为较高
int policy = SCHED_FIFO;
struct sched_param param;
param.sched_priority = 10; // 设置优先级,值越高优先级越高
pthread_attr_setschedpolicy(&attr, policy);
pthread_attr_setschedparam(&attr, ¶m);
// 创建线程
if (pthread_create(&thread, &attr, threadFunction, nullptr) != 0) {
std::cerr << "Failed to create thread!" << std::endl;
return 1;
}
// 销毁线程属性
pthread_attr_destroy(&attr);
// 其他主线程逻辑
// ...
// 等待线程结束
pthread_join(thread, nullptr);
return 0;
}
标签:优先级,attr,int,param,POSIX,pthread,线程
From: https://www.cnblogs.com/penuel/p/17445921.html