NSThread を使用して新しいスレッドを作成し、それから thread_funtion を実行しています。スレッドをキャンセルしたいので、[スレッド キャンセル] を使用しますが、スレッドは続行します。
NSThread *thread=[[NSThread alloc] initWithTarget:self selector:@selector(thread_funtion) object:nil];
[thread start];
-(void)thread_funtion
{
//my code
}
thread_funtion でフラグを使用してその実行を制御することは知っていますが、便利な場合もありますが、複雑すぎます。
-(void)thread_funtion
{
if(flag)
{
//some code
}
if(flag)
{
//some code
}
}
質問 1: スレッドをすぐにキャンセルするにはどうすればよいですか?</p>
次に、別の方法を使用して新しいスレッドを作成します。
pthread_t id;
int ret;
ret=pthread_create(&id,NULL,thread_function,NULL);
if(ret!=0){
printf ("Create pthread error!\n");
exit (1);
}
次に、pthread_cancel(id) を使用すると、スレッドはすぐに停止します。しかし、thread_function では:
void* thread_function(void *arg)
{
//This is not a function of the class, So I can't use self.somobject !
}
質問は、Cコードではなく、クラスのオブジェクトをどのように使用するかです。