javascript - Can an infinite loop be used? Or are we actually living in a world of endless cycles?
高洛峰
高洛峰 2017-05-31 10:34:05
0
6
859

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:

  1. php-resque There is an infinite loop inside. When I saw it, I instantly lost my favorable impression

  2. 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!

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(6)
迷茫

Is it that our computer has been in an infinite loop since it was turned on? Is the CPU just working in an infinite loop?

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.


What event/signal triggers a callback when it comes? How is it implemented? Is it also an infinite loop?

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.


Or maybe 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 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.


Please let me know what information and books I should read

How about the principles and implementation of computer composition and operating system?

PHPzhong

A sentence in "Ruby Metaprogramming" is very good, and I feel it is appropriate to put it here

There is no meta-programming at all, it has always been just programming.

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.

给我你的怀抱

Can we use infinite 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


Or are we actually living in a world of endless cycles?

In fact you can kill yourself to exit the cycle

Don’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

phpcn_u1582

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template