2

The following code causes bus error in Rasbian distro on a rasberry pi mod2 system.

#include <thread>
#include <iostream>

class bar {
public:
  void startFoo() {
   std::thread t(&bar::foo, this); //bus error because of this line
   t.join();
  }
  void foo() {
    std::cout << "hello from member function" << std::endl;
  }
};

int main()
{
  bar b;
  b.startFoo();
  return 0;
}

This link states that bus error occurs when your processor cannot even attempt the memory access requested. But in my code i am accessing the class own member function in thread. I cannot relate how it is causing bus error. Could someone clarify me? P.S. The source code was cross compiled on a Ubuntu OS running on a x86 PC and the binary was tested on Rasberry pi (ARM).

Community
  • 1
  • 1
Kumar
  • 616
  • 1
  • 18
  • 39
  • What command did you use to compile this? Did you specify `-pthread`? Did you specify `-std=c++11`? – David Schwartz Oct 19 '15 at 08:01
  • Bus error can happen for unaligned pointers, but I don't see that here. – ams Oct 19 '15 at 09:10
  • i have used ccache arm-linux-gnueabihf-g++ to cross compile. I have specified -std=c++0x and -pthread with compile command. – Kumar Oct 19 '15 at 09:10
  • Is it because i am using invalid compiler for cross compiling? The compiler which i am using now was not build for rasberry pi. – Kumar Oct 19 '15 at 09:12
  • Yeah, don't do that unless you know what you're doing. You'll need to get the CPU, ABI, and FPU settings right, and even then the compiler's built-in configuration settings might be incompatible. – ams Oct 19 '15 at 10:37
  • The code is harmless. Missing pthread and std++11 could never cause a sigbus. It must be the cross-compilation, it wasn't done right. – SergeyA Oct 19 '15 at 13:24
  • Chances are that it's an unaligned access of some sort, but that still leaves the question of which of the code, the compiler, or the kernel configuration are at fault. It would be helpful to know what the actual faulting instruction was and what it was trying to do. – Notlikethat Oct 19 '15 at 13:48
  • 2
    The bus error however was removed after i used the right cross compiler for raspberry pi. Can i add it as an answer? – Kumar Oct 24 '15 at 15:47

0 Answers0