In fact, asking this question is related to this question: How to do scheduled notification and release on time in PHP?
In the past, teachers always said that you should never write infinite loops when writing code. This is the biggest bug. This concept has always been ingrained in the mind, so when I see all kinds of infinite loops, I feel all kinds of contempt and entanglement.
But I found that many things actually use infinite loops:
php-resque There is an infinite loop inside. When I saw it, I instantly lost my favorable impression
After looking at the working principle of the browser, the JS event loop is actually an infinite loop, and the kernel code C seems to be an infinite loop
……
I don’t know how many things are in an infinite loop
Is it true that our computer has been in an endless loop since it was turned on? Is the CPU just working in an endless loop? Please help me clarify this. I am so confused now. If there is no endless loop, our code will actually be dead. If no one visits the website, it will be dead. What events/signals trigger callbacks when they come, and how are they implemented? Is it also an infinite loop?
Or we humans are in an endless loop, constantly receiving information and then giving feedback. Otherwise, if you call me and I can answer immediately, do you really think it was you who notified me? Do you have the ability to call me back? funny! I've been listening, okay?
I hope God can clear up the confusion, ^_^
Replenish:
When I read this information, I often see these words:
libevent
libev
PCNTL
EventLoop
timerfd
epoll
kqueue
select
poll
Please help me clarify my doubts. What information should I read and what books should I read? "Linux/UNIX Network Programming"
Thanks!
Is continuous work an infinite loop? The OS will fragment the CPU running time and allocate it to different processes. Every once in a while, the OS will take back control and then send it out again, and the cycle continues like this.
But this loop is not dead, you can end this loop by clicking
Shutdown
.Yes, Eventloop requires continuous monitoring, which can only be achieved in this way, and what we see is
Event occurrence
=>Execute callback
Such a natural thing, this is an abstraction.But it’s not dead, you can close the browser to end this cycle.
I think people are more like a state machine (FSM) than an infinite loop.
Change the state according to the input, thereby outputting the value (human dialogue)
I don’t know if the human brain executes instructions sequentially, but people can handle things with blurred boundaries, such as driving, cooking, listening, speaking, reading and writing in various languages, etc. People can withdraw from a certain thought at any time. Think about another thing, in other words, there are a lot of interrupt signals, maybe there is no such thing as an infinite loop, because it can be banned at any time.
How about the principles and implementation of computer composition and operating system?
A sentence in "Ruby Metaprogramming" is very good, and I feel it is appropriate to put it here
I think the same is true for infinite loops. Its existence is reasonable. It just depends on how you use it. When there is no better solution, it is important to solve the problem. When there is a better solution, code elegance is equally important. , there is no infinite loop at all, there is always only a loop.
Infinite loop != Infinite loop != Empty loop
Infinite loop is a bug, it will cause stuck
Infinite loop, such as: event loop
Empty loop, such as: spin wait
In fact you can kill yourself to exit the cycleDon’t explain
Don’t think wildly, it’s very middle-class behavior to talk about philosophy on all nonsense
Don’t become a folk philosopher
Have you ever written microcontroller code?
It’s while(1)
Exit by power off or interrupt
There is also a watchdog
You can think of the operating system as an infinite loop
This question is too grand and difficult to explain
But when you learn programming, you should try to avoid unnecessary infinite loops. It is best if the procedure has a beginning and an end, and the thinking is clear.
Instead of the infinite loop, it is better to say that the meta-loop has a beginning and an end like other loops, but other loops are a section of the meta-loop. Once you pass it, it will be gone. After the infinite loop has passed, you will return to the original point.