I
I
ishlambad2020-02-10 16:33:33
Objective-C
ishlambad, 2020-02-10 16:33:33

How does runloop tell a thread to sleep?

In the process of learning runloop-a, I came across this expression:

"The kernel transfers the thread to the pool of sleeping threads."

Question: Does
this happen automatically? That is, when the runloop goes into sleep mode, all work on the thread stops and the kernel automatically unloads the thread.

Let's say if I want to create my own thread, should I put it into hibernation mode, or do I not need to do this, because if the runloop falls asleep, then the thread will automatically be unloaded from the active thread pool in the kernel.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
doublench21, 2020-02-11
@ishlambad

You have a wrong understanding.
At its core - the thread performs its task and dies. But this is sometimes bad, because system resources are spent on creation. This problem can be solved in several ways...but one of them is RunLoop . RunLoop makes the thread stay alive even if no task is running on it (But this is not its main task. Its main task is to " listen "). RunLoop has become such a useful thing that Apple developers forced it to be tied to each created thread. By default, RunLoop is disabled. That is, for ordinary streams, you don’t even need to know about it. YES, you will not even create threads, because in 99% you work with abstractionsDispatchQueue / OperationQueue .
So, when RunLoop is activated, it makes the thread live for as long as it is active (I remind you that this is not the main goal for RunLoop ). RunLoop is capable of receiving any events, but this is not the topic of this conversation. So ... when RunLoop sees that the field is silent, it goes into sleep mode.
And the sleep mode of RunLoop is just an eternal loop. Don't believe? See :

#define	CFRUNLOOP_SLEEP() do { } while (0)
#define	CFRUNLOOP_SLEEP_ENABLED() (0)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question