2014-02-24 131 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