“fork()”的可移植方式 在Qt4应用程序中?

发布于 2024-07-23 08:59:01 字数 838 浏览 10 评论 0原文

比如说,我需要运行一堆容易崩溃的代码,所以我需要在不同的进程上运行它。 通常我会这样做:

  pid = fork();
  if (pid == -1) {
    std::cout << "Cant Spawn New Thread";
    exit(0);
  } else if (pid == 0) {
    std::cout << "Im a child that will crash\n";
    char *foo = (char *) 0xffff;
    std::cout << foo;
    exit(0);
  } else {
    std::cout << "PID:  " << pid << "\n";
  }
  do {
    std::cout << "Waiting for " << pid << "  to finish .. \n";
    pid_w = waitpid(pid,&status,0);
  } while (pid_w == -1);

显然我可以在我的 Qt4 应用程序中使用 fork,但我想知道我是否可以使用 Qt4 提供的任何东西或任何可移植的方式来归档相同的功能,而无需求助于一堆架构 #ifdefs ?

无论如何,我的目标是这个应用程序仅具有 pthread 实现,但我仍然希望保持尽可能接近“本机”Qt API。

我已经测试了 QThread,线程中的段错误显然会导致整个应用程序崩溃,并且 QProcess 似乎仅在生成完全不同的可执行文件时才使用。 还有其他选择吗?

Say, I need to run a bunch of code that is prone to crash so I need to run it on a different process. Typically I'd do it like this:

  pid = fork();
  if (pid == -1) {
    std::cout << "Cant Spawn New Thread";
    exit(0);
  } else if (pid == 0) {
    std::cout << "Im a child that will crash\n";
    char *foo = (char *) 0xffff;
    std::cout << foo;
    exit(0);
  } else {
    std::cout << "PID:  " << pid << "\n";
  }
  do {
    std::cout << "Waiting for " << pid << "  to finish .. \n";
    pid_w = waitpid(pid,&status,0);
  } while (pid_w == -1);

Obviously I can just use fork in my Qt4 application but I'm wondering if can I archive same functionality with any anything that Qt4 provides or any portable manner without resorting to having bunch of architecture #ifdefs ?

In any case, I'm targeting this app to have only pthread implementation but I'd still like to keep things as much close to "native" Qt API as possible.

I've tested QThread, and segfaulting in thread crashes the whole application obviously and it seems that QProcess is only targetted to be used when spawning completely different executables. Any other alternatives ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

滥情哥ㄟ 2024-07-30 08:59:01

Windows flat-out 没有任何公开可用的 fork() 方式,因此没有 Qt 调用来模拟它; 您需要做一些事情,例如使用特殊的命令行参数或其他东西来启动自己。

Windows flat-out doesn't have fork() in any publicly consumable way, so there's no Qt call to emulate it; you'll need to do something like start yourself with special command-line params or something.

我还不会笑 2024-07-30 08:59:01

我认为你应该使用 QtConcurrent 因为它是最高级的 API Qt 中提供多线程编程。 这样你的代码将会更加简单和干净。
由于这是一个高级 API,它可能是在您已经尝试过的较低级 API 之上实现的,因此这可能无法解决您的问题。

I think you should go with QtConcurrent as it's the most high-level API for multithreaded programming available in Qt. This way your code will be more simple and cleaner.
As this is a high-level API it's probably implemented on top of lower-level APIs you already tried so this probably may not solve your problem, however.

巡山小妖精 2024-07-30 08:59:01

您是否尝试过 try...catch 语句 & 弄清楚如何避免崩溃???

Have you tried try...catch statements & figuring out how to avoid crashing????

白馒头 2024-07-30 08:59:01

您也可以尝试 QThread 。 它比较旧,并且可能不如 QtConcurrent 方便。 它不会自动调整 CPU 上的核心数量,因此不会均匀地分配工作负载。 尽管如此,它可能是最接近 fork() 的 Qt 模拟,尽管它不是直接替代品,并且可能更适合您。 QThread 和 QtConcurrent 都可以跨平台移植。

You might try QThread also. It is older, and presumably less convenient than QtConcurrent. It does not adjust automatically for the number of cores on a CPU, and thus does not distribute the work-load as evenly. Still, it is probably the closest Qt analog to fork(), although it is not a drop-in replacement, and may work better for you. Both are QThread and QtConcurrent are portable across platforms.

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