实时线程和非实时线程测试

Posted s-ong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实时线程和非实时线程测试相关的知识,希望对你有一定的参考价值。

源码如下:

#include<stdio.h>
#include <unistd.h>
#include<pthread.h>

void* thread1(void* arg)
{
unsigned int i,j;

int policy;
struct sched_param param;
pthread_getschedparam( pthread_self( ) , &policy, &param) ;
if ( policy == SCHED_OTHER)
printf ( "SCHED_OTHER/n" ) ;
if ( policy == SCHED_RR)
printf ( "SCHED_RR /n" ) ;
if ( policy == SCHED_FIFO)
printf ( "SCHED_FIFO/n" ) ;

while(1)
{
for(i=0;i<2;i++)
{
printf("i am thread1,my tid is %lu ",pthread_self());
for(j=0;j<50000;j++);
}
printf(" ");
sleep(5);
}

}

void* thread2(void* arg)
{
unsigned int i,j;

int policy;
struct sched_param param;

pthread_getschedparam( pthread_self( ) , &policy, &param) ;
if ( policy == SCHED_OTHER)
printf ( "SCHED_OTHER/n" ) ;
if ( policy == SCHED_RR)
printf ( "SCHED_RR /n" ) ;
if ( policy == SCHED_FIFO)
printf ( "SCHED_FIFO/n" ) ;
while(1)
{
for(i=0;i<2;i++)
{
printf("i am thread2,my tid is %lu ",pthread_self());
for(j=0;j<50000;j++);
}
printf(" ");
sleep(5);
}
}


void* thread3(void* arg)
{
unsigned int i,j;

int policy;
struct sched_param param;
pthread_getschedparam( pthread_self( ) , &policy, &param) ;
if ( policy == SCHED_OTHER)
printf ( "SCHED_OTHER/n" ) ;
if ( policy == SCHED_RR)
printf ( "SCHED_RR /n" ) ;
if ( policy == SCHED_FIFO)
printf ( "SCHED_FIFO/n" ) ;

while(1)
{
for(i=0;i<2;i++)
{
printf("i am thread3,my tid is %lu ",pthread_self());
for(j=0;j<50000;j++);
}
printf(" ");
sleep(5);
}
}

int main()
{
pthread_t tid1,tid2,tid3;
pthread_attr_t attr1, attr2;
struct sched_param param;


pthread_attr_init ( &attr1) ;
pthread_attr_init ( &attr2) ;



param.sched_priority = 51;
pthread_attr_setschedpolicy ( &attr1, SCHED_RR) ;
pthread_attr_setschedparam ( &attr1, &param) ;
pthread_attr_setinheritsched ( &attr1, PTHREAD_EXPLICIT_SCHED ) ;

param.sched_priority = 21;
pthread_attr_setschedpolicy ( &attr2, SCHED_RR) ;
pthread_attr_setschedparam ( &attr2, &param) ;
pthread_attr_setinheritsched ( &attr2, PTHREAD_EXPLICIT_SCHED ) ;

pthread_create(&tid3,NULL,thread3,NULL);
pthread_create(&tid1,&attr1,thread1,NULL);
pthread_create(&tid2,&attr2,thread2,NULL);

pthread_join(tid3,NULL);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);


pthread_attr_destroy ( &attr1) ;
pthread_attr_destroy ( &attr2) ;

return 0;
}

 

结果如下:

# ./show_file
SCHED_RR /ni am thread1,my tid is 32771
i am thread1,my tid is 32771

SCHED_RR /ni am thread2,my tid is 49156
i am thread2,my tid is 49156

SCHED_OTHER/ni am thread3,my tid is 16386
i am thread3,my tid is 16386

i am thread1,my tid is 32771
i am thread1,my tid is 32771

i am thread2,my tid is 49156
i am thread2,my tid is 49156

i am thread3,my tid is 16386
i am thread3,my tid is 16386

i am thread1,my tid is 32771
i am thread1,my tid is 32771

i am thread2,my tid is 49156
i am thread2,my tid is 49156

i am thread3,my tid is 16386
i am thread3,my tid is 16386

i am thread1,my tid is 32771
i am thread1,my tid is 32771

i am thread2,my tid is 49156
i am thread2,my tid is 49156

i am thread3,my tid is 16386
i am thread3,my tid is 16386

i am thread1,my tid is 32771
i am thread1,my tid is 32771

i am thread2,my tid is 49156
i am thread2,my tid is 49156

i am thread3,my tid is 16386
i am thread3,my tid is 16386

i am thread1,my tid is 32771
i am thread1,my tid is 32771

i am thread2,my tid is 49156
i am thread2,my tid is 49156

i am thread3,my tid is 16386
i am thread3,my tid is 16386

i am thread1,my tid is 32771
i am thread1,my tid is 32771

i am thread2,my tid is 49156
i am thread2,my tid is 49156

i am thread3,my tid is 16386
i am thread3,my tid is 16386

































































































































以上是关于实时线程和非实时线程测试的主要内容,如果未能解决你的问题,请参考以下文章

C# 多线程 大量数据实时接收\解析\存储 问题

界面实时刷新线程信息

Java多线程线程安全和非线程安全

线程实时记录

如何在实时应用程序中锁定线程[关闭]

Opencv/c++ - 在线程中录制实时视频,在另一个线程中处理图像?