[英]iPhoneOS: using detachNewThreadSelector method inside a C++ class method
I have a C++ class method where i need to call the "detachNewThreadSelector" method with all the parameters. 我有一个C ++类方法,其中我需要使用所有参数来调用“ detachNewThreadSelector”方法。
Here lies the problem, as my class is not objective C i don't have a self pointer. 这就是问题所在,因为我的课程不是目标C,所以我没有自我指针。 Also i don't see how i will be able to call a class method from the method that i will set as selector. 我也看不到我将如何从我将设置为选择器的方法中调用类方法。
Please do ask if my question is not clear, i am not from a english speaking country. 请问我的问题是否不清楚,我不是来自英语国家。
Here is some code. 这是一些代码。
ALuint AudioController::PlayStream(const string& asset)
{
//attach to a thread
[NSThread detachNewThreadSelector:(SEL)selector toTarget:(id)selfwithObject:(id)argument]
}
void AudioController::RotateThread(const string& soundKey)
{
}
As you can see how do i pass the RotateThread method as a selector to the "detachNewThreadSelector" and also where do i get the self pointer. 如您所见,我如何将RotateThread方法作为选择器传递给“ detachNewThreadSelector”,以及从何处获取自指针。
Any help much appreciated. 任何帮助,不胜感激。
Thanks 谢谢
You can't do this. 你做不到 It isn't as a simple as "Where do I get the self pointer?" 它不像“我在哪里获得自指点?”那样简单。 The actual question is, "Where do I get something that can respond to messages?" 实际的问题是,“我在哪里可以获得可以响应消息的信息?” Because a C++ class can't . 因为C ++类不能 。
Objective-C classes, objects and methods are completely different things from C++ classes, objects and methods. Objective-C类,对象和方法与C ++类,对象和方法完全不同。 The fact that the two languages use the same terminology and use the things for similar purposes confuses a lot of people, but to be clear: They are totally different things that work in very different ways in the two languages. 两种语言使用相同的术语并且将事物用于相似的目的这一事实使很多人感到困惑,但请注意:它们是完全不同的事物,在两种语言中以不同的方式工作。 Case in point: C++ methods are simply called rather than dispatched based on a selector like Objective-C methods. 恰当的例子:简单地调用C ++方法,而不是基于像Objective-C方法这样的选择器进行分派。 And C++ classes aren't even objects. C ++类甚至不是对象。
You have two real options here: 您在这里有两个实际选择:
Create an Objective-C class that has the behavior you want. 创建具有所需行为的Objective-C类。
Use a C++ concurrency solution. 使用C ++并发解决方案。
you may not use c++ object in this manner (as an argument to this NSThread method). 您不能以这种方式使用c ++对象(作为此NSThread方法的参数)。 if your case is simple (read: few interfaces declared), then you can create a utility (objc) class to handle the message, and to then pass the argument back to the AudioController instance. 如果您的情况很简单(请参阅:声明了几个接口),则可以创建一个实用程序(objc)类来处理消息,然后将参数传递回AudioController实例。 illustration: 插图:
(non-compiled pseudo code follows) (后面是未编译的伪代码)
namespace pseudo_object {
template <typename> class reference_counted;
}
@interface MONAudioControllerWorker : NSObject
{
pseudo_object::reference_counted<AudioController> audioController_;
std::string asset_;
}
+ (MONAudioControllerWorker *)newMONAudioControllerWorkerWithAudioController:(pseudo_object::reference_counted<AudioController>&)audioController asset:(const std::string&)asset;
- (void)secondaryWorker;
@end
@implementation MONAudioControllerWorker
+ (MONAudioControllerWorker *)newMONAudioControllerWorkerWithAudioController:(pseudo_object::reference_counted<AudioController>&)audioController asset:(const std::string&)asset
{
/* ... */
}
- (void)secondaryWorker
{
NSAutoreleasePool * pool([NSAutoreleasePool new]);
audioController_->RotateThread(asset_);
[pool release];
}
@end
/* static */
ALuint AudioController::PlayStream(pseudo_object::reference_counted<AudioController>& This, const string& asset)
{
/* attach to a thread */
MONAudioControllerWorker * controller = [MONAudioControllerWorker newMONAudioControllerWorkerWithAudioController:This asset:asset];
[NSThread detachNewThreadSelector:@selector(secondaryWorker) toTarget:controller withObject:0];
[controller release];
}
sometimes it is just easier to create an objc class which may contain a simplified (generic) interface for this purpose (ie reusable beyond this object), or to use more traditional threading routines (pthreads). 有时,创建一个objc类(其中可能包含一个简化的(通用)接口)来达到目的(即,可以在此对象之外重用),或者使用更传统的线程例程(pthreads),都比较容易。 if this is the only case in the project, then it should be fine. 如果这是项目中唯一的情况,那就应该没问题。 otherwise, you end up with many utility classes/symbols and much more to maintain. 否则,您将获得许多实用程序类/符号,还有更多需要维护的类。 illustration: 插图:
@interface MONAudioControllerWrapper : NSObject
{
AudioController audioController_;
std::string asset_;
}
+ (MONAudioControllerWrapper *)newMONAudioControllerWrapperWithAsset:(const std::string&)asset;
- (void)playStream;
@end
@implementation MONAudioControllerWrapper
+ (MONAudioControllerWrapper *)newMONAudioControllerWrapperWithAsset:(const std::string&)asset
{
/* ... */
}
- (void)secondaryWorker
{
NSAutoreleasePool * pool([NSAutoreleasePool new]);
audioController_->RotateThread(asset_);
[pool release];
}
- (void)playStream
{
[NSThread detachNewThreadSelector:@selector(secondaryWorker) toTarget:self withObject:0];
}
@end
As others have said, you can't use detachThreadWithSelector: passing a C++ object as a target or using a C++ method as the selector. 正如其他人所说,您不能使用detachThreadWithSelector:将C ++对象作为目标传递,或将C ++方法用作选择器。
You have two strategies: 您有两种策略:
wrap the object and selector with an OBjective-C object and selector eg 用OBjective-C对象和选择器包装对象和选择器,例如
myAudioControllerWrapper = [[OCAudioControllerWrapper alloc] initWithRealController: this]; // need some code to release once thread is complete [NSThread detachNewThreadSelector: @selector(wrapperSelector:) target: myAudioControllerWrapper withObject: soundKeyAsNSObject];
and your wrapper selector looks like: 您的包装选择器如下所示:
-(void) wrapperSelector: (id) key { cppController->rotateThread([key cppObject]); }
Use some other thread mechanism more in keeping with the C++ paradigm. 为了与C ++范例保持一致,请更多使用其他线程机制。 Grand Central Dispatch might be the one if your platform supports it. 如果平台支持,则可以使用Grand Central Dispatch 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.