【问题标题】:pthread_cancel behaves differently on arm and ppc?pthread_cancel 在 arm 和 ppc 上的行为不同?
【发布时间】:2009-03-12 09:12:38
【问题描述】:

我目前正在开发一个多线程应用程序,该应用程序将部署在 arm 和 ppc 架构上。我在 arm 上的 pthread_cancel 遇到了一些问题。

arm 上的 pthread_cancel 与 ppc 的行为不同。线程被取消,但线程局部变量的析构函数没有在 arm 上调用。我还尝试明确定义通过 pthread_cleanup_push 安装的取消清理处理程序例程。但是当线程被取消时它不会被调用。

代码在 ppc 上运行良好。当线程被取消时,将调用局部变量的析构函数。而当我明确定义一个清理处理程序时,它会在调用 pthread_cancel 时被调用并执行。

我错过了什么吗?也许有一些编译器选项?

  • 编程语言:C++
  • 编译器:arm-linux-g++/powerpc-linux-g++
  • 操作系统:Linux

编辑:

我在libc bug 上发现了类似的问题。

使用 gcc 而不是 g++ 并添加 -fno-exception 编译器选项就可以了。但我真的很想了解这个问题背后的东西。此外,-fno-exception 意味着我将无法在我的应用程序中执行异常处理,不是我现在正在使用它,而是我将来可能会使用它。

谢谢。

【问题讨论】:

  • 请指定使用的语言和编译器(用于两个平台)。这听起来像 C++,但最好是明确的。
  • 这也有助于了解哪个操作系统,因为 pthread 包通常是特定于操作系统的。
  • 我还建议列出 Linux 内核的确切版本、每种情况下使用的目标平台支持以及 gcc/g++、libc 和涉及的任何其他软件的版本。如果没有准确的版本号,很难给出好的建议。

标签: embedded pthreads arm


【解决方案1】:

在没有应用程序帮助的情况下取消线程是个坏主意。只需google。最好通过设置一个由线程定期检查的标志变量来告诉线程自行结束。

实际上取消是如此困难,以至于在最新的 C++0x 草案中已将其省略。你可以搜索http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html,根本找不到任何关于取消的信息。这是提议的线程类的定义(你不会在那里找到取消):

class thread
{
public:
    // types:
    class id;
    typedef implementation-defined native_handle_type; // See [thread.native]

    // construct/copy/destroy:
    thread();
    template <class F> explicit thread(F f);
    template <class F, class ...Args> thread(F&& f, Args&&... args);
    ~thread();
    thread(const thread&) = delete;
    thread(thread&&);
    thread& operator=(const thread&) = delete;
    thread& operator=(thread&&);

    // members:
    void swap(thread&&);
    bool joinable() const;
    void join();
    void detach();
    id get_id() const;
    native_handle_type native_handle(); // See [thread.native]

    // static members:
    static unsigned hardware_concurrency();
};

【讨论】:

  • 我接受了这样的事情。我只是决定远离 pthread_cancel 以及它可能带来的所有混乱。我只是添加了一个标志变量来表示线程的状态,并检查了一些指定为取消点的区域的状态。我还发送了一个 SIGINT 来阻止 I/O 调用。这样我就可以完全控制取消点,并且线程将能够一直干净而优雅地终止。谢谢!
猜你喜欢
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
  • 2010-12-22
  • 2015-11-29
  • 2017-09-20
  • 2010-10-31
  • 2021-04-26
  • 2019-05-24
相关资源
最近更新 更多