2014-02-24 81 views
0

我正在努力寻找一种用新数据填充缓冲区的好方法。我有一个线程从声卡生成数据,我想通过一个名为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'; 
} 
+4

你想打印'p',指针。你正在打印'&p'又名“哪里有'p'存储”,这可能每次都是一样的 – turbulencetoo

+0

啊哈!非常感谢。但是,如果另一个线程正在从同一个地址读取数据,那么会出现问题?它对我来说都是新的指针和东西.. – user3348461

+0

是的。会有问题。但同步是一个很大的问题。您应该阅读文档,或者至少打开其他问题。 – Avt

回答

1

你应该只是印刷p,不&p,因为p已经包含了新的结构的地址。你正在做的是打印p变量的地址,每次调用该函数时都很容易相同。

0

,而不是使用

std::cout << "Adressse fra Streamhandler: " << p <<'\n'; 

std::cout << "Adressse fra Streamhandler: " << &p <<'\n'; 

p是已经指针。你不需要在这里接受它的地址。

+0

谢谢!你有没有在一个线程中分配内存并在另一个线程中删除它的经验? – user3348461

+0

你想给我一份工作吗?如果没有,你应该开始用谷歌搜索。这个链接可能是一个开始http://en.cppreference.com/w/cpp/thread/lock。 – Avt

+0

呵呵呵。还没有工作,但感谢您的链接。 – user3348461