我正在努力寻找一种用新数据填充缓冲区的好方法。我有一个线程从声卡生成数据,我想通过一个名为Rawcontainer的共享对象与其他线程共享这些数据。该容器容纳一个互斥体和一个环缓冲区,但是当我尝试填充缓冲区时,我注意到我填充缓冲区的对象都具有相同的内存地址,从而使整个缓冲区无用。多次创建新结构
void useBuffer(){
//Create a new "OBject" (struct) each time this methos is called??
SoundData *p = new SoundData();
//Copy the data of the sound into the the struct data field
memcpy(p->data, soundCopy, 2048*sizeof(double));
//Put the struct into the buffer and forget about it?
Rawcontainer->writeRaw(p);
//This should print a differnt adress each time the method is called?, but it dosent!
std::cout << "Adressse fra Streamhandler: " << &p <<'\n';
}
你想打印'p',指针。你正在打印'&p'又名“哪里有'p'存储”,这可能每次都是一样的 – turbulencetoo
啊哈!非常感谢。但是,如果另一个线程正在从同一个地址读取数据,那么会出现问题?它对我来说都是新的指针和东西.. – user3348461
是的。会有问题。但同步是一个很大的问题。您应该阅读文档,或者至少打开其他问题。 – Avt