While(1) 在构造函数中还是使用线程?
是否建议放置一个永远不会在构造函数中结束的 while 循环?或者我应该使用线程来获得相同的结果? 当构造函数永远不会终止时,这很好吗?或者避免分段错误更安全?
希望你能理解我糟糕的英语..
Is it recommed to put a while loop, which never ends in a constructor? Or should I use threads to get the same result?
Is it good when a constructor never terminates? Or is it more secure to avoid segmentation faults?
Hope you understand my bad English..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果对象的构造函数未完成,则该对象不存在。因此,在构造函数中放置 while(1) 循环将阻止使用该构造函数创建对象。您需要描述您认为这样做可以解决什么问题。
An object does not exist if its constructor does not finish. So putting a while(1) loop in a constructor will prevent objects being created using that constructor. You need to describe what problem you think doing this will solve.
不。
不。
不。
不,
你想解决什么问题?
No.
No.
No.
No.
What problem are you trying to solve?
我想说,从构造函数中生成线程是完全可以的,但在构造函数中出现无限循环是一个非常糟糕的主意。
在代码中,以下内容是可以的:
以下内容至少是一个坏主意:
不过,好消息是,由于尼尔指出的原因,您可能无法使用这样的对象:)
I'd say it's perfectly OK to spawn a thread from a constructor, and a horribly bad idea to have an endless loop in the constructor.
In code, the following is OK:
And the following would be at least a bad idea:
Good thing though, is that likely you wouldn't be able to use such object, for reasons Neil pointed out :)
无论您的代码是否使用多个线程或包含无限循环,都不应生成分段错误。
这些都与分段错误无关。如果发生这些情况,您需要解决该问题
Your code shouldn't generate a segmentation fault whether or not it uses multiple threads or contains infinite loops.
None of those have anything to do with segmentation faults. If those occur, you need to fix that problem
在嵌入式系统中,应该避免大多数无限循环。它们应该是定时的或计数的迭代。如果循环终止,则应执行错误检查。这可以避免系统被“锁定”。
该规则的一个例外是后台循环。这是检查事件并保持机器运行的主循环。如果没有这个循环,机器可能会终止或停止。
In embedded systems, most endless loops should be avoided. They should either be timed or counted iterations. If the loop is terminated, an error check should be performed. This refrains from the system being "locked up".
One exception to the rule is the background loop. This is the main loop in which events are checked and keeps the machine running. Without this loop, machines may just terminate or halt.