[C] 纯文本查看 复制代码#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#pragma comment(lib,"pthreadVC2.lib") //必不可少,这是告诉编译器在编译形成的.obj文件和.exe文件中加一条信息,使得链接器在链接库的时候要去找pthreadVC2.lib这个库,不要先去找别的库。(.exe文件找DLL 也是这种写法,例如 pthreadVC2.dll)[color=#ff0000]通过设置和加上这句可以在vs中运行linux中的代码[/color]
//线程优先级调度
#if 1
static int api_get_thread_policy(pthread_attr_t *attr)
{
int policy;
int rs = pthread_attr_getschedpolicy(attr, &policy);
assert(rs == 0);
switch (policy)
{
case SCHED_FIFO:
printf("policy = SCHED_FIFO\n");
break;
case SCHED_RR:
printf("policy = SCHED_RR");
break;
case SCHED_OTHER:
printf("policy = SCHED_OTHER\n");
break;
default:
printf("policy = UNKNOWN\n");
break;
}
return policy;
}
static void api_show_thread_priority(pthread_attr_t *attr, int policy)
{
int priority = sched_get_priority_max(policy);
assert(priority != -1);
printf("max_priority = %d\n", priority);
priority = sched_get_priority_min(policy);
assert(priority != -1);
printf("min_priority = %d\n", priority);
}
static int api_get_thread_priority(pthread_attr_t *attr)
{
struct sched_param param;
int rs = pthread_attr_getschedparam(attr, ¶m);
assert(rs == 0);
printf("priority = %d\n", param.sched_priority);
return param.sched_priority;
}
static void api_set_thread_policy(pthread_attr_t *attr, int policy)
{
int rs = pthread_attr_setschedpolicy(attr, policy);
assert(rs == 0);
api_get_thread_policy(attr);
}
int main(void)
{
pthread_attr_t attr; // 线程属性
struct sched_param sched; // 调度策略
int rs;
/*
* 对线程属性初始化
* 初始化完成以后,pthread_attr_t 结构所包含的结构体
* 就是操作系统实现支持的所有线程属性的默认值
*/
rs = pthread_attr_init(&attr);
assert(rs == 0); // 如果 rs 不等于 0,程序 abort() 退出
/* 获得当前调度策略 */
int policy = api_get_thread_policy(&attr);
/* 显示当前调度策略的线程优先级范围 */
printf("Show current configuration of priority\n");
api_show_thread_priority(&attr, policy);
/* 获取 SCHED_FIFO 策略下的线程优先级范围 */
printf("show SCHED_FIFO of priority\n");
api_show_thread_priority(&attr, SCHED_FIFO);
/* 获取 SCHED_RR 策略下的线程优先级范围 */
printf("show SCHED_RR of priority\n");
api_show_thread_priority(&attr, SCHED_RR);
/* 显示当前线程的优先级 */
printf("show priority of current thread\n");
int priority = api_get_thread_priority(&attr);
/* 手动设置调度策略 */
printf("Set thread policy\n");
printf("set SCHED_FIFO policy\n"); //[color=#ff0000]SCHED_RR 或 SCHED_FIFO 策略需要使用 sudo 权限运行程序,不知道是不是因为在vs中才导致这里和下面的设置返回值不为零,即设置失败?[/color]
api_set_thread_policy(&attr, SCHED_FIFO);
printf("set SCHED_RR policy\n");
api_set_thread_policy(&attr, SCHED_RR);
/* 还原之前的策略 */
printf("Restore current policy\n");
api_set_thread_policy(&attr, policy);
/*
* 反初始化 pthread_attr_t 结构
* 如果 pthread_attr_init 的实现对属性对象的内存空间是动态分配的,
* phread_attr_destory 就会释放该内存空间
*/
rs = pthread_attr_destroy(&attr);
assert(rs == 0);
return 0;
}
#endif