gpt4 book ai didi

c++ - Oboe C++ 线程——如何在不阻塞的情况下读取和写入队列

转载 作者:行者123 更新时间:2023-12-03 06:59:52 26 4
gpt4 key购买 nike

我正在尝试使用双簧管为音频系统编写缓冲区,该做和不该做

Callback do's and don'ts You should never perform an operation whichcould block inside onAudioReady. Examples of blocking operationsinclude:

allocate memory using, for example, malloc() or new;

file operations such as opening, closing, reading or writing;

network operations such as streaming;

use mutexes or other synchronization primitives sleep

stop or close the stream

Call read() or write() on the stream which invoked it

音频线程从我的缓冲区中读取,解码器线程写入它,正如您想象的那样,在线程问题出现之前一切正常。我的主要问题是我只能使用互斥锁来解决这个问题,但如果我这样做,我会阻塞其中一个线程,如果音频线程被阻塞,则基本上不会播放声音,从而导致“爆米花”声。 (一个听起来很令人不安的声音)

我通过向其提供数据的回调来播放声音。

DataCallbackResult
OboeStreamCallback::onAudioReady(AudioStream *audioStream, void *audioData, int32_t numFrames) {
// fill data here
return DataCallbackResult::Continue;
}

所以我的主要问题是如何解决从音频线程读取数据又不阻塞音频线程以便它仍然可以播放音频的问题?

这对我来说是不可能的。没有互斥锁,如何保证线程安全?双簧管如何期望您不使用互斥体进行动态音频解码?

最佳答案

您可以使用线程安全的无锁队列来读取/写入数据。这将避免对互斥锁的需要,速度会快得多,并且应该可以解决您的“爆米花”问题。

可在此处找到示例实现:https://github.com/google/oboe/blob/master/samples/RhythmGame/src/main/cpp/utils/LockFreeQueue.h

关于c++ - Oboe C++ 线程——如何在不阻塞的情况下读取和写入队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64957941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com