将类作为参数传递给NSThread



如何使用NSThread将类作为参数传递给线程。在windows中,我做了如下操作:

DWORD WINAPI threadFunc(LPVOID mpThis) {
MYCLSS *pThis = reinterpret_cast<MYCLSS*>(mpThis);
....
void MYCLSS::func() {
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadFunc, (void*)this, 0, NULL);
....

对于Mac,我还没有找到任何这样的例子。请帮帮我

我找到了解决方案,它可以像windows

class CLS_CPP {
public:
void Func();
void Check();
int var_check;
};
@interface CLS_OSX : NSObject {
@public
void *obj;
}
-(void)osx_thread;
@end
@implementation CLS_OSX
-(void)osx_thread {  
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
CLS_CPP *pThis = reinterpret_cast<CLS_CPP*>(obj);
pThis->Check();
NSLog(@"Check var %d", pThis->var_check);
[NSThread exit];
[pool drain];
}
@end
void CLS_CPP::Func() {
var_check=77;
CLS_OSX *trgt=[[CLS_OSX alloc] init]; 
trgt->obj=(void*)this;
[NSThread detachNewThreadSelector:@selector(osx_thread) toTarget:trgt withObject:trgt];
}
void CLS_CPP::Check() {
NSLog(@"CLS_CPP::Check success");
}
int main(int argc, char ** argv) {

CLS_CPP oAPP;
oAPP.Func();  

}

最新更新