核心音频渲染线程和线程信令 [英] Core Audio render thread and thread signalling

查看:74
本文介绍了核心音频渲染线程和线程信令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS是否具有不包含锁定的任何一种非常低级的条件锁定?

Does iOS have any kind of very low level condition lock that does not include locking?

我正在寻找一种方法来从Core Audio渲染线程中发信号通知正在等待的线程,而无需使用锁.我想知道是否可能存在低级的Mach系统调用.

I am looking for a way to signal an awaiting thread from within the Core Audio render thread, without the usage of locks. I was wondering if something low level as a Mach system call might exist.

现在,我有一个Core Audio线程,该线程使用非阻塞线程安全消息队列将消息发送到另一个线程.然后另一个线程每100ms拉一次,以查看队列中是否有消息.

Right now I have a Core Audio thread that uses a non-blocking thread safe message queue to send messages to another thread. The other thread then pulls every 100ms to see if messages are available in the queue.

但是,这是非常基本的,时机也很糟糕.我可以使用条件锁,但这涉及到锁定,我想将任何类型的锁定都保留在呈现线程之外.

But this is very rudimentary and the timing is awful. I could use condition locks, but that involves locking, and I would like to keep any kind of locking out of the rendering thread.

我正在寻找的是让消息队列线程等待核心音频渲染线程发出信号.就像pthread条件一样,但是没有锁定并且没有立即上下文切换?我希望Core Audio线程在消息队列线程被唤醒之前完成.

What I am looking for is having the message queue thread wait until the Core Audio render thread signals it. Just like pthread conditions, but without locking and without immediate context switching? I would like the Core Audio thread to complete before the message queue thread is woken up.

推荐答案

已更新

A dispatch_semaphore_t效果很好,并且比马赫semaphore_t效率更高.原始代码使用调度信号如下所示:

Updated

A dispatch_semaphore_t works well and is more efficient than a mach semaphore_t. The original code looks like this using a dispatch semaphore:

#include <dispatch/dispatch.h>

// Declare mSemaphore somewhere it is available to multiple threads
dispatch_semaphore_t mSemaphore;


// Create the semaphore
mSemaphore = dispatch_semaphore_create(0);
// Handle error if(nullptr == mSemaphore)


// ===== RENDER THREAD
// An event happens in the render thread- set a flag and signal whoever is waiting
/*long result =*/ dispatch_semaphore_signal(mSemaphore);


// ===== OTHER THREAD
// Check the flags and act on the state change
// Wait for a signal for 2 seconds
/*long result =*/ dispatch_semaphore_wait(mSemaphore, dispatch_time(dispatch_time_now(), 2 * NSEC_PER_SEC));


// Clean up when finished
dispatch_release(mSemaphore);

原始答案:

您可以为此使用马赫数semaphore_t.我编写了一个封装该功能的C ++类: https://github.com /sbooth/SFBAudioEngine/blob/master/Semaphore.cpp

Original answer:

You can use a mach semaphore_t for this purpose. I've written a C++ class that encapsulates the functionality: https://github.com/sbooth/SFBAudioEngine/blob/master/Semaphore.cpp

无论您是否最终使用我的包装程序或滚动自己的代码,代码都将大致如下:

Whether or not you end up using my wrapper or rolling your own the code will look roughly like:

#include <mach/mach.h>
#include <mach/task.h>

// Declare mSemaphore somewhere it is available to multiple threads
semaphore_t mSemaphore;


// Create the semaphore
kern_return_t result = semaphore_create(mach_task_self(), &mSemaphore, SYNC_POLICY_FIFO, 0);
// Handle error if(result != KERN_SUCCESS)


// ===== RENDER THREAD
// An event happens in the render thread- set a flag and signal whoever is waiting
kern_return_t result = semaphore_signal(mSemaphore);
// Handle error if(result != KERN_SUCCESS)


// ===== OTHER THREAD
// Check the flags and act on the state change
// Wait for a signal for 2 seconds
mach_timespec_t duration = {
  .tv_sec = 2,
  .tv_nsec = 0
};

kern_return_t result = semaphore_timedwait(mSemaphore, duration);

// Timed out
if(KERN_OPERATION_TIMED_OUT != result)
  ;

// Handle error if(result != KERN_SUCCESS)


// Clean up when finished
kern_return_t result = semaphore_destroy(mach_task_self(), mSemaphore);
// Handle error if(result != KERN_SUCCESS)

这篇关于核心音频渲染线程和线程信令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆