While(1) 在构造函数中还是使用线程?

发布于 2024-08-16 06:53:27 字数 110 浏览 5 评论 0原文

是否建议放置一个永远不会在构造函数中结束的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

放肆 2024-08-23 06:53:27

如果对象的构造函数未完成,则该对象不存在。因此,在构造函数中放置 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.

诺曦 2024-08-23 06:53:27

是否建议放置一个永远不会在构造函数中结束的 while 循环?

不。

或者我应该使用线程来获得相同的结果吗?

不。

构造函数永远不会终止是好事吗?

不。

或者避免分段错误更安全吗?

不,

你想解决什么问题?

Is it recommed to put a while loop, which never ends in a constructor?

No.

Or should I use threads to get the same result?

No.

Is it good when a constructor never terminates?

No.

Or is it more secure to avoid segmentation faults?

No.

What problem are you trying to solve?

帅气称霸 2024-08-23 06:53:27

我想说,从构造函数中生成线程是完全可以的,但在构造函数中出现无限循环是一个非常糟糕的主意。

在代码中,以下内容是可以的:

void start_a_thread_with_a_loop()
{
    while(1)
    {
         // consider a while(!stop_condition) or something
         // do something in a loop
    }
}

class x
{
public:
    x()
    {
        start_a_thread_with_a_loop();
    }
};

以下内容至少是一个坏主意:

class x
{
public:
    x()
    {
        while(1)
        {
        }
    }
};

不过,好消息是,由于尼尔指出的原因,您可能无法使用这样的对象:)

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:

void start_a_thread_with_a_loop()
{
    while(1)
    {
         // consider a while(!stop_condition) or something
         // do something in a loop
    }
}

class x
{
public:
    x()
    {
        start_a_thread_with_a_loop();
    }
};

And the following would be at least a bad idea:

class x
{
public:
    x()
    {
        while(1)
        {
        }
    }
};

Good thing though, is that likely you wouldn't be able to use such object, for reasons Neil pointed out :)

妖妓 2024-08-23 06:53:27

无论您的代码是否使用多个线程或包含无限循环,都不应生成分段错误。

这些都与分段错误无关。如果发生这些情况,您需要解决该问题

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

想你的星星会说话 2024-08-23 06:53:27

在嵌入式系统中,应该避免大多数无限循环。它们应该是定时的或计数的迭代。如果循环终止,则应执行错误检查。这可以避免系统被“锁定”。

该规则的一个例外是后台循环。这是检查事件并保持机器运行的主循环。如果没有这个循环,机器可能会终止或停止。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文