简体   繁体   English

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

[英]Oboe C++ Threads - How to read and write to a queue without blocking

I am trying to write a buffer for an audio system with Oboe, the do and don'ts are我正在尝试为带有双簧管的音频系统编写缓冲区,注意事项是

Callback do's and don'ts You should never perform an operation which could block inside onAudioReady .回调该做什么和不该做什么 您永远不应该执行可能在onAudioReady内部onAudioReady的操作。 Examples of blocking operations include:阻塞操作的例子包括:

allocate memory using, for example, malloc() or new;例如,使用malloc()或 new 分配内存;

file operations such as opening, closing, reading or writing;文件操作,如打开、关闭、读取或写入;

network operations such as streaming;网络操作,如流媒体;

use mutexes or other synchronization primitives sleep使用互斥锁或其他同步原语 sleep

stop or close the stream停止或关闭流

Call read() or write() on the stream which invoked it在调用它的流上调用read()write()

Audio thread reads from my buffer and decoder thread writes to it and as you can imagine its all good until threading issues kicks in. My main problem is I can just use a mutex to overcome this issue but if i do so I will be blocking one of the threads and if audio thread is blocked then the sound basically not played resulting in "popcorn" sound.音频线程从我的缓冲区读取,解码器线程写入它,你可以想象这一切都很好,直到线程问题出现。我的主要问题是我只能使用互斥锁来解决这个问题,但如果我这样做,我将阻止一个线程,如果音频线程被阻塞,那么声音基本上不会播放,导致“爆米花”声音。 (A Sound that is so disturbing to listen to) (一个听着很烦人的声音)

I play the sound through a callback where I feed the data to it.我通过回调播放声音,在那里我将数据提供给它。

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

So my main issue is how may I solve read data from audio thread also not block audio thread so it can still play audio?所以我的主要问题是如何解决从音频线程读取数据的问题,同时又不阻塞音频线程,因此它仍然可以播放音频?

This sounds impossible to me.这对我来说听起来不可能。 How may one can assure thread safety without a mutex?如何在没有互斥锁的情况下确保线程安全? How does oboe expect you to not use mutex for dynamic audio decoding?双簧管如何期望您不使用互斥锁进行动态音频解码?

You can use a thread-safe lock-free queue for reading/writing data.您可以使用线程安全的无锁队列来读取/写入数据。 This will avoid the need for mutexes, will be much faster and should fix your "popcorn" issues.这将避免对互斥锁的需要,速度会更快,并且应该可以解决您的“爆米花”问题。

An example implementation can be found here: https://github.com/google/oboe/blob/master/samples/RhythmGame/src/main/cpp/utils/LockFreeQueue.h可以在此处找到示例实现: https : //github.com/google/oboe/blob/master/samples/RhythmGame/src/main/cpp/utils/LockFreeQueue.h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM