LabWindows:实现可以处理字符串元素的线程安全队列 [英] LabWindows: implementing thread safe queues that can handle string elements

查看:75
本文介绍了LabWindows:实现可以处理字符串元素的线程安全队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Labwindows 服务器,它在远程机器上侦听来自 python 客户端的数据.在 Labwindows 服务器上,每当触发 TCP_DATAREADY 事件时,我都会读取缓冲区以从客户端获取数据.此时我需要解析数据并更新 UI 控件.但是,由于数据频繁到达,最好将解析数据和更新 UI 控件的任务分配给不同的线程,这样服务器就不会丢失来自客户端的任何数据.

I have a Labwindows server that listens for data from a python client on a remote machine. On the Labwindows server, whenever the TCP_DATAREADY event is fired, I read the buffer to get the data from the client. At this point I need to parse the data and update UI controls. However, since data arrives frequently, it is best if I can assign the task of parsing the data and updating UI controls to a different thread, so the server won't lose any data from the client.

我曾想过在 Labwindows 中使用线程安全队列.但是,我从客户端获取的数据是一个字符串.如何构造一个包含字符串元素的队列?我不能使用指针,因为每次数据到达时都会更新从客户端收到的数据.

I have thought of using thread safe queues in Labwindows. However, the data I get from the client is a string. How do I construct a queue which has string elements? I can't use pointers as the data received from the client is updated everytime data arrives.

这是从套接字读取数据的一段代码:

Here is the piece of code that reads data from the socket:

case TCP_DATAREADY:
            if ((dataSize = ServerTCPRead (g_hconversation, receiveBuf,
                                           dataSize, 1000))
                < 0)
                {
                SetCtrlVal(g_hconversation, MAINPNL_TEXTBOX,"Receive Error");
        }
            else
                {
        //receiveBuff should be passed to function that decides where to display it
        display_value_from_client(receiveBuf);

                }

因此,receiveBuf 不断变化,将指向它的指针保存在队列中无济于事.我如何在这里实现线程化,以便我的线程负责解析功能,而我只需要担心从主线程中的套接字读取数据?

So, receiveBuf is constantly changing and saving pointers to it in the queue won't help. How do I implement threading here so my threads take care of the parsing functionality and I only need to worry about reading data from the socket in the main thread?

推荐答案

执行此类线程间通信的经典"方式是 malloc 接收缓冲区,在收到数据后从缓冲区指针排队,并立即分配另一个接收缓冲区用于下一次数据加载.您可以在 GUI 中处理缓冲区指针后释放它们.

The 'classic' way of performing such inter-thread comms is to malloc the receiveBuf, queue off the buffer pointer after receipt of data and immediately malloc another receive buffer for the next load of data. You can free the buffer pointers after handling them in the GUI.

这种方法意味着 UI 和网络线程永远不会在同一个缓冲区上运行,因此可以独立运行.

Such an approach means that the UI and network threads never operate on the same buffer and so can operate independently.

这篇关于LabWindows:实现可以处理字符串元素的线程安全队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆