gpt4 book ai didi

c++ - 在 PPC 中创建线程时崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:57 25 4
gpt4 key购买 nike

我已经使用 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 ()

能否请您提出建议,我们是否在构建标志中遗漏了一些内容。

最佳答案

你的代码中有一个明显的错误

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

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

关于c++ - 在 PPC 中创建线程时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37568815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com