整整一天,我試圖找到解決方案,沒有任何結果。你是唯一的希望。我不得不做基於bass.h庫的聲音合成器。一切工作正常,直到我想它播放單獨的流。我決定做它在單獨的線程:C++線程 - 崩潰
void Synthezator::play(MonoStream *stream1, MonoStream *stream2)
{
threadParam data = {stream1, this};
threadParam data2 = {stream2, this};
int x = 6;
DWORD threadId;
DWORD threadId2;
//thread first (PlayThread, data);
//thread second (PlayThread, data2);
//first.join();
//second.join();
CreateThread(NULL, 0, PlayThread, (PVOID) &data, 0, &threadId);
CreateThread(NULL, 0, PlayThread, (PVOID) &data2, 0, &threadId2);
getchar();
}
(以同樣的方式評價那些作品)
的崩潰隨機occures(隨機的時刻,不是每次)僅當兩個線程都在工作。如果我評論其中之一 - 沒有崩潰。
PlayThread功能:
DWORD WINAPI Synthezator::PlayThread(PVOID data)
{
//cout << "kurwa";
int i=0;
threadParam* dataNew = (threadParam*) data;
dataNew->stream->SeekBegin();
while(!(dataNew->stream->eof()))
{
int no = 0;
note* a = dataNew->stream->Next(&no);
dataNew->pointer->PlayNote(a, no);
i++;
}
}
PlayNote:
void Synthezator::PlayNote(note* note, int count)
{
position = new double[count];
generatorParams params = {synthType, count, note, position, this};
HSTREAM SignalStream = BASS_StreamCreate (FREQUENCY, 1, 0, &Generator, ¶ms);
if (SignalStream == 0) throw " Cannot create stream";
if(note->sound != 0) {
if (!BASS_ChannelPlay (SignalStream, TRUE)) throw "Cannot play generated sample";
}
const int secinusecs = 1000000;
sleep(secinusecs*floor(note->duration)/1000000);
usleep(secinusecs*(note->duration-floor(note->duration)));
if(note->sound != 0) {
BASS_ChannelStop(SignalStream);
}
BASS_StreamFree(SignalStream);
delete [] position;
}
如果我評論這一行,沒有崩潰(也沒有聲音):
if (!BASS_ChannelPlay (SignalStream, TRUE)) throw "Cannot play generated sample";
通過PlayThread中的第一個cout來判斷您一直在努力:D – AccessViolation
聲明位置變量在哪裏?它從2個線程同時修改! – AccessViolation
刪除行「delete [] position;」解決了這個問題。謝謝! – user1678401