I need to create an NSThread
object with a C++ class
object passed. The problem is to "exit" this thread from the class the thread operates with.
I tried:
int threadProc(void* mpClass);
struct my_autopool { /// sterge valoarea obiectelor NS definite in functie/constructor
my_autopool() { pool = [[NSAutoreleasePool alloc] init]; }
~my_autopool() { [pool release]; }
NSAutoreleasePool *pool;
};
@interface NS_THD : NSObject {
@public
void* thobj;
}
@end
@implementation NS_THD
-(void)thread_start {
hreadProc(thobj);
}
-(void)thread_close {
printf("Close!\n");
[NSThread close];
}
@end
class CLS_THD {
public:
NSThread* mythread;
NS_THD *trgt;
void CreateThreadOsx(void* mpClass) {
my_autopool ap;
trgt=[[NS_THD alloc] init];
trgt->thobj=(void*)mpClass;
mythread = [[NSThread alloc] initWithTarget:trgt selector:@selector(thread_start) object:nil];
[mythread start];
}
void ExitThreadOsx() {
[mythread close];
//[trgt thread_close];
}
};
class CLS_CPP {
CLS_THD th;
public:
int var_check;
CLS_CPP() {
printf("Hello CLS_CPP!\n");
var_check=77;
th.CreateThreadOsx((void*)this);
}
void change() {
var_check++;
}
void exit() {
th.ExitThreadOsx();
}
};
int threadProc(void* mpClass) {
CLS_CPP *pClass = reinterpret_cast<CLS_CPP*>(mpClass);
while([[NSThread currentThread] isCancelled] == NO) {
printf("Check var %d\n", pClass->var_check);
usleep(333000);
}
return 0;
}
The key issue i'm struggling right now is that it seems there is no way to "close" the NSThread
object.
How to solve this situation?