基于两个程序之间的事件触发器访问数据(内存)

Accessing data (memory) based on event trigger between two programs

本文关键字:数据 访问 内存 触发器 事件 于两个 程序 之间      更新时间:2023-10-16

这个问题对一些人来说可能很基本,但如果有人能在这里澄清我的疑虑,那就太好了。我有两个软件程序(两个进程),它们通过网络通过标准TCP连接进行通信。例如,程序A就像一个服务器,不断等待来自程序B的指令和数据。一旦程序A接收到数据,它就会立即将数据存储在向量数组中,并执行一个函数,该函数使用数组中的数据,并在最后将其清除。例如,在程序A内部:

std::vector<int> m_dataArray;
Run()
{
m_received = false;
while(1)
{
if(m_received)
{
Execution();
}
}
}
Listener(int data)
{
m_dataArray.push_back(data);    
m_received = true;
}
Execution()
{
m_received = false;
// read from the data array and do something
m_dataArray.clear();
}

我的问题是,如果程序B一直向程序A发送数据,程序A是否会将所有这些传入数据放入缓冲区,直到程序A完成当前执行,然后再将新的数据集放入阵列?否则它将在访问内存的同时出现问题。(例如,即使程序A仍处于执行阶段,m_received标志是否会一直闪烁?)

如果这将成为一个问题,那么实施这样的措施以避免任何问题的最佳方式是什么?我想让程序A在发送下一个数据之前向程序B发出一个"完成"信号。

下面是一个例子,说明Run和Execution在执行时,侦听器可能会做什么,而不会有任何问题

std::vector<int> m_dataArray;
Run()
{
while(1)
{
if(m_dataArrayBuffer.length > 0)
{
// we take the data out and use it in execute
// these operations are independent of listener adding additional data
int data = m_dataArrayBuffer.pop();
Execution(data);
}
}
}
Listener(int data)
{
m_dataArrayBuffer.push_back(data);    
}
Execution(int data)
{
// user the data and do something
}