Mbed OS多线程问题

本人使用mbed os 5.7.4遇到有关线程的问题,望请教!

程序如下,

问题:当三个线程任意一个start时,功能OK。但两个以上线程start时,只会运行第一个线程。

#include "mbed.h"
#include "rtos.h"

Thread thread1;                //线程定义
Thread thread2;
Thread thread3;

DigitalOut red_light(PTB19);    //红灯
DigitalOut blue_light(PTB9);    //蓝灯
DigitalOut green_light(PTB18);  //绿灯
Serial pc(PTE0,PTE1);           //UART1

//======================================================================
//函数名称:red_thread
//函数返回:无
//参数说明:led:线程使用的外设
//功能概要:每2秒红灯反转
//内部调用:
//======================================================================
void red_thread(void)
{
    while (true) {
    	wait(2);
    	red_light = !red_light;
        pc.printf("红灯反转\n");

    }
}
//======================================================================
//函数名称:blue_thread
//函数返回:无
//参数说明:led:线程使用的外设
//功能概要:串口接收到0x11,蓝灯反转
//内部调用:
//======================================================================
void blue_thread(void)
{
    while (true) {
    	Thread::signal_wait(0x11);
    	blue_light = !blue_light;
        pc.printf("蓝灯反转\n");
    }
}
//======================================================================
//函数名称:green_thread
//函数返回:无
//参数说明:led:线程使用的外设
//功能概要:串口接收到0x22,蓝灯反转
//内部调用:
//======================================================================
void green_thread(void)
{
    while (true) {
    	Thread::signal_wait(0x22);
    	green_light = !green_light;
        pc.printf("绿灯反转\n");
    }
}
//======================================================================
//函数名称:uart_isr
//函数返回:无
//参数说明:无
//功能概要:串口接收中断服务例程(自定义),(1)数据回发功能(2)接收到0x11给线程2发送信号量0x11
//                            (3)接收到0x22给线程3发送信号量0x22
//内部调用:
//======================================================================
void uart_isr(void)
{
	int c = pc.getc();
	pc.putc(c);

	if(c == 0x11)
		thread2.signal_set(0x11);
	else if(c == 0x22)
		thread3.signal_set(0x22);
}

//主函数
int main()
{
	red_light = 1;  //三色灯默认关闭
	blue_light = 1;
	green_light = 1;

	//定义串口接收中断函数
	pc.attach(&uart_isr,SerialBase::RxIrq);

	//串口测试数据
	pc.printf("MCU start1\n");

	//led灯闪烁线程开启
	thread1.start(red_thread);
	thread2.start(blue_thread);
	thread3.start(green_thread);

	//串口测试数据
	pc.printf("MCU start2\n");

	//主循环
    while (true) {

    }
}

Parents Reply Children
No data