在PPC中创建线程的崩溃

时间:2016-06-01 12:36:10

标签: c++ linux powerpc

我已经使用PPC工具链为以下程序构建了可执行文件。

工具链详情: powerpc-wrs-linux-gnu-g ++(Wind River Linux Sourcery G ++ 4.4a-341)4.4.1

我们在编译时包含了-pthread,在链接中包含了-lpthread。我们也使用-lrt和-ldl标志。

#include <string>
#include <iostream>
#include <thread>

using namespace std;

// The function we want to execute on the new thread.
void task1(string msg)
{
    cout << "task1 says: " << msg;
}

int main()
{
    // Constructs the new thread and runs it. Does not block execution.
    thread t1(task1, "Hello");

    // Makes the main thread wait for the new thread to finish execution
    // therefore blocks its own execution.
    t1.join();
 }

执行程序时遇到崩溃,如下所示

Program received signal SIGILL, Illegal instruction.
0x10000e30 in __gnu_cxx::__exchange_and_add(int volatile*, int) ()
(gdb) bt
#0  0x10000e30 in __gnu_cxx::__exchange_and_add(int volatile*, int) ()
#1  0x10000f14 in __gnu_cxx::__exchange_and_add_dispatch(int*, int) ()
#2  0x10001960 in    std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() ()
#3  0x100016ac in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count() ()
#4  0x100013ac in std::__shared_ptr<std::thread::_Impl_base, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr() ()
#5  0x100013e8 in std::shared_ptr<std::thread::_Impl_base>::~shared_ptr() ()
#6  0x100014c0 in std::thread::thread<void (&)(std::basic_string<char, std::char_traits<char>, std::allocator<char> >), char const (&) [6]>(void (&&&)(std::basic_string<char, std::char_traits<char>, std::allocator<char> >), char const (&&&) [6]) ()
#7  0x10000fd4 in main ()

你能否建议我们在标志中遗漏了什么东西。

2 个答案:

答案 0 :(得分:0)

您的代码中存在一个明显的错误

cout << "task1 says: " << msg;

这里cout(stream)是共享资源,你应该同步访问它。

答案 1 :(得分:0)

主要提示是:

Program received signal SIGILL, Illegal instruction.

看起来编译器的默认代码生成设置正在输出CPU不支持的指令。如果从gdb打印实际的错误指令,则可以获得有关该问题的更多详细信息。尝试:

(gdb) x /i $pc

查看导致SIGILL的确切说明。

由于非法指令在__exchange_and_add,因此可能是原子存储指令之一。

要解决此问题,您可能需要告诉编译器哪个CPU生成指令。您可以使用-mcpu=参数执行此操作。如果给出无效的cpu说明符,gcc将打印可用的CPU类型:

$ powerpc64le-linux-gnu-gcc -mcpu=?
powerpc64le-linux-gnu-gcc: error: unrecognized argument in option ‘-mcpu=?’
powerpc64le-linux-gnu-gcc: note: valid arguments to ‘-mcpu=’ are: 401 403 405 405fp 440 440fp 464 464fp 476 476fp 505 601 602 603 603e 604 604e 620 630 740 7400 7450 750 801 821 823 8540 8548 860 970 G3 G4 G5 a2 cell e300c2 e300c3 e500mc e500mc64 e5500 e6500 ec603e native power3 power4 power5 power5+ power6 power6x power7 power8 power9 powerpc powerpc64 powerpc64le rs64 titan
powerpc64le-linux-gnu-gcc: fatal error: no input files