VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 914|回复: 1

Concurrency::unbounded_buffer 怎么用?

[复制链接]
35_avatar_middle
最佳答案
0 
online_vip 发表于 2020-5-14 03:56:06 | 显示全部楼层 |阅读模式
我在别的工程文件看到这样的代码,但不晓得什么意思,主要是Concurrency::unbounded_buffer不晓得怎么用。

class MESSAGE_STACK
{
public:
    Concurrency::unbounded_buffer<MESSAGE> msgs;

    int WaitingCalls = 0; // Number of threads waiting
    bool Destroy = false; // Destroy stack as soon as possible
};

// Allocate a message stack
MESSAGE_STACK* MsgAllocStack()
{
    return new MESSAGE_STACK();
}

// Free a message stack
void MsgFreeStack(MESSAGE_STACK* Stack)
{
    ASSERT_NONNULL(Stack);

    // Update termination variable
    Stack->Destroy = true;

    // Notify each thread
    for(int i = 0; i < Stack->WaitingCalls + 1; i++) //TODO: found crash here on exit
    {
        MESSAGE newMessage;
        Stack->msgs.enqueue(newMessage);
    }

    // Delete allocated structure
    //delete Stack;
}

// Add a message to the stack
bool MsgSend(MESSAGE_STACK* Stack, int Msg, duint Param1, duint Param2)
{
    if(Stack->Destroy)
        return false;

    MESSAGE newMessage;
    newMessage.msg = Msg;
    newMessage.param1 = Param1;
    newMessage.param2 = Param2;

    // Asynchronous send
    asend(Stack->msgs, newMessage);
    return true;
}

// Get a message from the stack (will return false when there are no messages)
bool MsgGet(MESSAGE_STACK* Stack, MESSAGE* Msg)
{
    if(Stack->Destroy)
        return false;

    // Don't increment the wait count because this does not wait
    return try_receive(Stack->msgs, *Msg);
}

// Wait for a message on the specified stack
void MsgWait(MESSAGE_STACK* Stack, MESSAGE* Msg)
{
    if(Stack->Destroy)
        return;

    // Increment/decrement wait count
    InterlockedIncrement((volatile long*)&Stack->WaitingCalls);
    *Msg = Stack->msgs.dequeue();
    InterlockedDecrement((volatile long*)&Stack->WaitingCalls);
}



百度了一下Concurrency::unbounded_buffer很少,具体的功能我也不太清楚。




上一篇:对话框的背景设置
下一篇:滚动条设置
97_avatar_middle
最佳答案
2 
在线会员 发表于 2020-5-14 10:09:47 | 显示全部楼层
摘自 http://msdn.microsoft.com/zh-cn/library/dd728068(v=vs.110).aspx

本主题描述如何在您的应用程序中实现制造者-使用者模式。 在此模式中,制造者向消息块发送消息,使用者从该块中读取消息。

本主题演示了两种方案。 在第一个方案中,使用者必须接收制造者发送的每条消息。 在第二个方案中,使用者定期轮询数据,因此不必接收每条消息。

本主题中的两个示例均使用代理、消息块和消息传递函数将消息从制造者传输给使用者。 制造者代理程序使用 concurrency::send 函数以写入到消息 concurrency::ITarget对象。 客户工程师使用 concurrency::receive 函数,查看来自 concurrency::ISource 对象。 两个代理都具有 sentinel 值以协调处理结束。

有关异步代理的更多信息,请参见异步代理。 有关消息块和消息传递函数的更多信息,请参见异步消息块和消息传递函数。

示例
在本示例中,制造者代理将一系列数字发送给使用者代理。 使用者接收其中每个数字并计算它们的平均值。 应用程序将平均值写入控制台。

本示例使用 concurrency::unbounded_buffer 对象启用队列邮件制造者。 unbounded_buffer 类实现 ITarget 和 ISource,以便制造者和使用者可以将消息发送到共享缓冲区以及从共享缓冲区接收消息。 send 和 receive 函数会对将数据从制造者传播给使用者这一任务进行协调。
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-6-4 21:25

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表