Windows Message Queue————栈和队列(优先队列)

Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue. Meanwhile, the process will do a loop for getting message from the queue according to the priority value if it is not empty. Note that the less priority value means the higher priority. In this problem, you are asked to simulate the message queue for putting messages to and getting message from the message queue.
Input
There’s only one test case in the input. Each line is a command, “GET” or “PUT”, which means getting message or putting message. If the command is “PUT”, there’re one string means the message name and two integer means the parameter and priority followed by. There will be at most 60000 command. Note that one message can appear twice or more and if two messages have the same priority, the one comes first will be processed first.(i.e., FIFO for the same priority.) Process to the end-of-file.
Output
For each “GET” command, output the command getting from the message queue with the name and parameter in one line. If there’s no message in the queue, output “EMPTY QUEUE!”. There’s no output for “PUT” command.
Sample Input
GET
PUT msg1 10 5
PUT msg2 10 4
GET
GET
GET
Sample Output
EMPTY QUEUE!
msg2 10
msg1 10
EMPTY QUEUE!



#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<iostream> 
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;

struct node{
    string name;
    int x,a,b;
    node(){}
    node(string _name,int _x,int _a,int _b)
    {
        name=_name;
        x=_x;
        a=_a;
        b=_b;
    }
    bool operator <(const node & n)const 
    {
        return a==n.a?b>n.b:a>n.a;
    }
};



int main() 
{
    string s;
    priority_queue<node> q;
    ios::sync_with_stdio(false);
    int y=0;
    while(cin>>s)
    {
        if(s[0]=='G')
        {   

            if(q.size())
            {
                node z=q.top();
                q.pop();
                cout<<z.name<<" "<<z.x<<endl;
            }
            else
                cout<<"EMPTY QUEUE!"<<endl;
        }
        else
        {
            y++;
            string na;
            int a,b;
            cin>>na>>a>>b;
            q.push(node(na,a,b,y));
        }
    }
    return 0;
}
### FreeRTOS在STM32 CubeMX中的消息队列使用教程 FreeRTOS是一款轻量级的实时操作系统,广泛应用于嵌入式系统开发中。在STM32CubeMX工具中,可以通过图形化界面配置FreeRTOS的相关功能,包括任务管理、信号量、消息队列等。以下是关于FreeRTOS在STM32 CubeMX中的消息队列使用的详细说明。 #### 消息队列的基本概念 消息队列是一种用于任务间通信的机制,允许一个或多个任务向队列发送消息,同时允许一个或多个任务从队列接收消息。消息队列可以存储任意类型的数据,并且支持不同长度的消息[^2]。 #### STM32CubeMX中配置FreeRTOS消息队列 1. **创建新项目并配置时钟源** 在STM32CubeMX中新建一个项目,选择目标MCU型号后进入主界面。配置系统的时钟源(SYS Timebase Source),通常可以选择TIMx或者SysTick作为定时器来源[^1]。 2. **启用FreeRTOS组件** 在“Middleware”选项卡中找到FreeRTOS组件并勾选启用。根据需求选择CMSIS_V1或CMSIS_V2版本,这两种版本的主要区别在于API的兼容性实现方式[^2]。 3. **生成代码并打开工程文件** 配置完成后点击“Generate Code”按钮生成初始化代码。随后使用STM32CubeIDE或其他IDE打开生成的工程文件。 #### 消息队列的实现与使用 以下是一个简单的示例,展示如何在STM32CubeMX生成的FreeRTOS项目中使用消息队列: ```c #include "main.h" #include "cmsis_os.h" osMessageQueueId_t queueHandle; void StartDefaultTask(void *argument) { uint32_t message = 0; while (1) { // 向队列发送消息 osMessageQueuePut(queueHandle, &message, 0, 0); HAL_Delay(500); // 从队列接收消息 if (osMessageQueueGet(queueHandle, &message, NULL, 0) == osOK) { HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // 切换LED状态 } } } void MX_FREERTOS_Init(void) { // 创建消息队列 queueHandle = osMessageQueueNew(10, sizeof(uint32_t), NULL); // 创建任务 osThreadNew(StartDefaultTask, NULL, NULL); } ``` 上述代码中,`osMessageQueueNew`函数用于创建一个新的消息队列,参数分别表示队列长度每个消息的大小(以字节为单位)。`osMessageQueuePut``osMessageQueueGet`分别用于向队列发送接收消息。 #### 注意事项 - 确保消息队列的长度消息大小设置合理,避免内存不足的问题。 - 在多任务环境中,确保消息队列的操作是线程安全的,FreeRTOS内部已经实现了必要的同步机制[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值