rajeshkb
rajeshkb

Reputation: 41

Crash for thread creation in PPC

I have build executable for below program with PPC toolchain.

Tool chain details: powerpc-wrs-linux-gnu-g++ (Wind River Linux Sourcery G++ 4.4a-341) 4.4.1

We have included -pthread during compilation and -lpthread for linking. We are using -lrt and -ldl flags too.

#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();
 }

While executing the program am getting the crash as below

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 ()

Can you please suggest are we missing something in flags for build.

Upvotes: 1

Views: 446

Answers (2)

Jeremy Kerr
Jeremy Kerr

Reputation: 1935

The main hint is here:

Program received signal SIGILL, Illegal instruction.

It looks like the default code generation settings of your compiler are outputting instructions that are not supported by your CPU. If you print the actual faulting instruction from gdb, you can get more detail about the problem. Try:

(gdb) x /i $pc

To see the exact instruction causing the SIGILL.

Since the illegal instruction is in __exchange_and_add, it's likely that this will be one of the atomic storage instructions.

To fix this, you will probably want to tell your compiler which CPU to generate instructions for. You can do this with the -mcpu= argument. If you give an invalid cpu specifier, gcc will print the available CPU types:

$ 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

Upvotes: 0

mystic_coder
mystic_coder

Reputation: 472

There is one obvious bug in your code

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

here cout (stream) is shared resource , you should synchronize access to it.

Upvotes: 0

Related Questions