如何通过对象退出 NSThread

问题描述 投票:0回答:0

我需要创建一个传递类对象的 NSThread。问题是从开始时传递的类中退出该线程。

我试过:

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 {
  threadProc(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(true) {
    printf("Check var %d\n", pClass->var_check);
    usleep(333000);
  }

  return 0;
}

无法关闭 NSThread。我已经尝试了一切,如果我取消注释 //[trgt thread_close];结果是一样的。很奇怪 [NSThread close] 真的存在?

如何解决这种情况?

感谢您的帮助! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

c++ objective-c nsthread
© www.soinside.com 2019 - 2024. All rights reserved.