我在测试rt-thread 在龙芯2k上线程切换所需的花销,我的思路是创建1000个线程,所创将的线程都存放在全局静态数组中,线程中的内容是将自身挂起,如果线程被唤醒,将会唤醒下一个线程,然后再第一个和最后一个线程中测算1000个线程所需的时钟,循环测试一个小时,代码如下
#include
#include
#define THREAD_PRIORITY 25
#define THREAD_STACK_SIZE 512
#define THREAD_tiMESLICE 5 #define THREAD_MAX 1000
#define THREAD_TEST_TIME 1000*60*60
static rt_thread_t tid[THREAD_MAX] ;
static rt_tick_t old_tk,new_tk;
static double max_tk = 0,min_tk = 0,avg_tk = 0;
static void thread1_entry(void *parameter)
{
rt_uint64_t flag=RT_NULL;
rt_uint64_t num = (rt_uint64_t)parameter;
double spend_tk;
if(num==0){
rt_kprintf(\"start supend ---------------------\\n\");
}else if(num==THREAD_MAX-1){
rt_kprintf(\"end supend ----------------------\\n\");
}
flag = rt_thread_suspend(rt_thread_self());
if(flag!=RT_EOK){
rt_kprintf(\"thread%d is suspend failed\\n\",num);
}
rt_schedule();
if(num==0){
old_tk = rt_tick_get();
rt_kprintf(\"thread start---------------\\n\");
}else if(num==THREAD_MAX-1){
new_tk = rt_tick_get();
rt_kprintf(\"thread end------------\\n\");
spend_tk = (1.0*new_tk-old_tk)/THREAD_MAX;
printf(\"old time = %d,new time = %d,spend %f ms do change task \\n\",old_tk,new_tk,spend_tk);
// rt_kprintf(\" old time = %d,new time = %d,spend %f ms do change task \\n\",old_tk,spend_tk);
if(min_tk==0||min_tk>spend_tk){
min_tk = spend_tk;
}
if(max_tk==0||max_tk(THREAD_TEST_TIME)){
break;
}
}
printf(\"do change task %ld times, mins_task = %f ms,max_task = %f ms,avg_task = %f ms \\n\",count*1000,min_tk,max_tk,avg_tk/count);
// thread_switch_test();
}
/* export the msh command */
MSH_CMD_EXPORT(thread_switch, thread switch);
最后再测试的过程中,前十分钟的运行没什么问题,但之后会出现切换线程的时间大幅度的增加,由原来的0.006ms 突然变成 5ms 左右,而且在运行一段时间后,还会线程创建失败,之后我将线程数量改成500就没有出现这样的问题,请各位指点一下是什么原因