运行 单独的音频引擎 thread/process,从主线程发送和接收消息
Running audio engine in separate thread/process, sending and receiving messages from main thread
我正在使用 C++ 内置的独立音频引擎编写 iOS 应用程序。我们的想法是尽可能多地将应用程序编写在 Swift、运行 音频引擎中作为单独的线程或进程,然后让用户界面触发与音频引擎的通信。
如何最好地实现这一目标?
我的第一个尝试是添加一个中间 Objective-C++ class (TestEngine) 来处理触发适当的 C++ 代码。这个 Objective-C++ class 我在 Swift 中启动是这样的:
// Initialize audio engine
let engine = TestEngine()
// Start engine in a new thread
NSThread.detachNewThreadSelector(NSSelectorFromString("startEngine"), toTarget: engine, withObject: nil)
测试引擎 class 看起来像这样:
@implementation TestEngine
- (void) startEngine {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(selectorMethod)
name:@"testSelector"
object:nil];
NSLog(@"StartEngine done on thread:%@", [NSThread currentThread]);
}
- (void) selectorMethod {
NSLog(@"This is selector, on thread %@", [NSThread currentThread]);
}
- (void)dealloc
{
NSLog(@"Now deallocing");
}
@end
现在的问题是对象在能够收到任何通知之前就被立即释放了。我也曾尝试研究 RunLoop,但未能找到任何简单(不是太低级)的方法来将观察者添加到 RunLoop。关于如何在 iOS 上最好地设计这样的解决方案有什么想法吗?
我想这就是你的答案
引用 Apple 文档
Regular notification centers deliver notifications on the thread in
which the notification was posted. Distributed notification centers
deliver notifications on the main thread. At times, you may require
notifications to be delivered on a particular thread that is
determined by you instead of the notification center. For example, if
an object running in a background thread is listening for
notifications from the user interface, such as a window closing, you
would like to receive the notifications in the background thread
instead of the main thread. In these cases, you must capture the
notifications as they are delivered on the default thread and redirect
them to the appropriate thread.
我正在使用 C++ 内置的独立音频引擎编写 iOS 应用程序。我们的想法是尽可能多地将应用程序编写在 Swift、运行 音频引擎中作为单独的线程或进程,然后让用户界面触发与音频引擎的通信。
如何最好地实现这一目标?
我的第一个尝试是添加一个中间 Objective-C++ class (TestEngine) 来处理触发适当的 C++ 代码。这个 Objective-C++ class 我在 Swift 中启动是这样的:
// Initialize audio engine
let engine = TestEngine()
// Start engine in a new thread
NSThread.detachNewThreadSelector(NSSelectorFromString("startEngine"), toTarget: engine, withObject: nil)
测试引擎 class 看起来像这样:
@implementation TestEngine
- (void) startEngine {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(selectorMethod)
name:@"testSelector"
object:nil];
NSLog(@"StartEngine done on thread:%@", [NSThread currentThread]);
}
- (void) selectorMethod {
NSLog(@"This is selector, on thread %@", [NSThread currentThread]);
}
- (void)dealloc
{
NSLog(@"Now deallocing");
}
@end
现在的问题是对象在能够收到任何通知之前就被立即释放了。我也曾尝试研究 RunLoop,但未能找到任何简单(不是太低级)的方法来将观察者添加到 RunLoop。关于如何在 iOS 上最好地设计这样的解决方案有什么想法吗?
我想这就是你的答案
引用 Apple 文档
Regular notification centers deliver notifications on the thread in which the notification was posted. Distributed notification centers deliver notifications on the main thread. At times, you may require notifications to be delivered on a particular thread that is determined by you instead of the notification center. For example, if an object running in a background thread is listening for notifications from the user interface, such as a window closing, you would like to receive the notifications in the background thread instead of the main thread. In these cases, you must capture the notifications as they are delivered on the default thread and redirect them to the appropriate thread.