First, then the previous article through the Nsthread can easily create a thread, and start the thread of the Runloop, a while loop in the process body
And then we'll be able to make use of this thread conveniently.
-(void) ThreadRun: (Nsthread *) thread{ NSLog (@ "thread Run"); NSLog (@ "Hello 1"); while (!_iscancelled && [Currentrunloop runmode:nsdefaultrunloopmode beforedate:[nsdate distantfuture]) { NSLog (@ "Hello 2"); _iscancelled = [CurrentThread iscancelled]; } NSLog (@ "Hello 3");}
Second, how should we properly end a nsthread?
Nsthread end can call two methods, one is Object method-[thread Cancel], this method marks Nsthraed property iscanceled is yes, then by matching the above loop, the thread body itself is over.
Another method is +[thread exit], note that this method needs to be called in nsthraed for the current thread, which compares violence regardless of whether the task in the thread is still executing,
The direct end, regardless of the runloop, can lead to a memory leak.
Third, Nsrunloop
-(BOOL) RunMode: (nsrunloopmode) mode beforedate: (NSDate *) limitdate;
According to the official documentation, the above API will return no immediately when there is no event source or timer in Runloop, so it is necessary to join a timer when executing the thread body loop.
Ensure that the while loop does not end, and that the thread does not end
-(void) ThreadRun: (Nsthread *) thread{ NSLog (@ "thread Run"); _livetimer = [Nstimer scheduledtimerwithtimeinterval:[[nsdate distantfuture] Timeintervalsincenow] target:self selector: @selector (ignore:) Userinfo:nil Repeats:yes]; Nsthread *currentthread = [Nsthread CurrentThread]; Nsrunloop *currentrunloop = [Nsrunloop currentrunloop]; _iscancelled = [CurrentThread iscancelled]; NSLog (@ "Hello 1"); while (!_iscancelled && [Currentrunloop runmode:nsdefaultrunloopmode beforedate:[nsdate distantfuture]) { NSLog (@ "Hello 2"); _iscancelled = [CurrentThread iscancelled]; NSLog (@ "Hello 4"); } NSLog (@ "Hello 3");}
The meaning of the above means that, as long as the loop ends, the line Cheng ends, then the nsthread ends, and the Runloop in Nsthread is over, regardless of what events in Runloop have been done
So once the runloop is up, will the Runloop end automatically after all the events in Runloop have been removed?
-(void) Stopthread: (Nsthread *) thread{//[self performselector: @selector (stopthread:) Withobject:thread Afterdelay:5 ]; [_livetimer invalidate]; Cfrunloopstop (Cfrunloopgetcurrent ()); [Thread Cancel]; _iscancelled = YES; while ([thread isexecuting] = = YES) usleep (10000); NSLog (@ "Stop done"); }-(void) Ignore: (Nstimer *) timer{NSLog (@ "Ignore");} -(void) ThreadRun: (Nsthread *) thread{NSLog (@ "thread run"); Consumes 0% CPU _livetimer = [Nstimer scheduledtimerwithtimeinterval:[[nsdate distantfuture] Timeintervalsincenow] Target:self selector: @selector (ignore:) Userinfo:nil Repeats:yes]; Nsthread *currentthread = [Nsthread CurrentThread]; Nsrunloop *currentrunloop = [Nsrunloop currentrunloop]; _iscancelled = [CurrentThread iscancelled]; NSLog (@ "Hello 1"); while (/*!_iscancelled &Amp;& */[currentrunloop runmode:nsdefaultrunloopmode beforedate:[nsdate Distantfuture]] {NSLog (@ "Hello 2"); _iscancelled = [CurrentThread iscancelled]; NSLog (@ "Hello 4"); } NSLog (@ "Hello 3");}
The above code removes the cancel tag, so even if the timer is removed, the while is still in circulation because-(BOOL) RunMode: (nsrunloopmode) mode beforedate: (NSDate *) Limitdate; The second value is not returned
So if there is no while loop, then the thread body is really over.
How IOS keeps threads running (ii)