Compiling with -march causes threads to say "pure virtual method called" - c++

I am trying to compile my C++ Raspberry Pi 3 code through gcc with a -march=armv8-a flag. However, using this flag causes my threads to fail by saying pure virtual method called. I know it is the -march=armv8-a flag because when I compile it without it, the threads start working again.
Please note: The thread does not even start, it just gives a pure virtual method called
Can someone compile this with -march=armv8-a on their Raspberry Pi 3 and report back on what they got?
#include <iostream>
#include <thread>
#include <unistd.h>
void threadedFunction()
{
std::cout << "Hello from thread" << std::endl;
}
int main()
{
std::thread t1(threadedFunction);
sleep(2);
return 0;
}

Since armv8-a uses a 64-bit architecture, using the -march=armv8-a flag is going to compile for a 64-bit machine. However, many Raspberry Pi OS(images) are 32-bit which may cause crashes or errors.
Credit: https://stackoverflow.com/users/1505939/m-m

Related

c++ throw with try catch all always hits terminate in C++ 11 14 and 17

I keep getting terminate called for anything I throw using GCC 9.2, even if it is caught.
terminate called after throwing an instance of 'char const*'
terminate called recursively
I have tested -std=c++17, -std=c++14, -std=c++11
Example test:
#include <iostream>
int main()
{
try
{
throw "not found";
}
catch(...)
{
std::cout << "test" << std::endl;
}
return 0;
}
It doesn't fail if i compile using visual studio or on multiple of the online compilers.
example:
https://repl.it/languages/cpp
https://www.onlinegdb.com/online_c++_compiler
I have also tried putting the throw in a function and adding noexcept(false), but this fails as well. Example:
#include <iostream>
void foo() noexcept(false)
{
throw std::runtime_error( "test1" );
}
int main()
{
try
{
foo();
}
catch(...)
{
std::cout << "test2" << std::endl;
}
return 0;
}
Edit:
System info:
I'm using 9-2020-q2-update - arm-linux-none-gnueabihf.
Basically, the setup is Linux x86 as my main computer, cross compiling for ARM Cortex-A processor. The processor i'm testing are Raspberry Pi 4 and BeagleBone Black.
The code compiles correctly and runs fine on the target processor, except when an exception is hit. At which point, it hits terminate for any throw.
I'm using Eclipse as the IDE, using remote debug to upload and step through the code on either of the target processors.
It seems there is a bug or exception handling isn't working on version 9.2 of GCC (ARM only?) compiler.
I tried with version 8.3-2019.03 - arm-linux-gnueabihf - Linux x86 compiler and they are working just fine. No other changes were necessary, other than the compile switch.
https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads

Thread could not be resolved in eclipse 4.7

I've installed MinGW(gcc 6.3.0) and Eclipse 4.7 on Windows 8.1. I could compile and run other programs without any issue. But when I was using thread it is showing that thread was not declared in this scope. I've tried this and this but both no avail. Could somebody help me fix this?
Here is the code I've treid.
#include <iostream>
#include <thread>
void foo(){
std::cout<<"thread function \n";}
int main(void)
{
std::thread t1(foo);
t1.join();
return 0;
}
I'm not sure about the C++11 features. I've tried Lambda function example from this page(last example) and it is working fine.

boehm-gc with C++11's thread library

As we know, using boehm-gc in multi-thread requires calling GC_register_my_thread with stack base from GC_get_stack_base. but It seems not to work well with C++11's thread library, such as std::thread... How can I use boehm-gc with C++11's thread library?
(I use VS2013)
edit: This is tested code. std::thread is good, but std::future doesn't work (stop on _CrtIsValidHeapPointer
#include <iostream>
#include <thread>
#include <future>
#define GC_THREADS
#include <gc.h>
#include <gc_cpp.h>
#pragma comment(lib, "gcmt-lib")
void foo()
{
GC_stack_base sb;
GC_get_stack_base(&sb);
GC_register_my_thread(&sb);
int *ptr;
for (int i = 0; i < 10; i++)
{
ptr = new (GC) int;
*ptr = 1;
}
GC_unregister_my_thread();
}
int main()
{
GC_INIT();
GC_allow_register_threads();
std::cout << "test for std::thread";
std::thread thrd(foo);
thrd.join();
std::cout << " [sucs]\n";
std::cout << "test for std::future";
std::future<void> fu = std::async(std::launch::async, foo);
fu.get();
std::cout << " [sucs]\n";
std::cin.get();
}
edit: here is a capture of stack trace (Sorry that it isn't English, but I think it doesn't matter, anyway)
and here is a debug message
HEAP[TestGC.exe]: Invalid address specified to RtlValidateHeap( 00E80000, 00C92F80 )
While debugging, I found The error occurs after fu.get().
edit: The error doesn't occur with /MD(or /MDd)...
(I think GC might touch library's pointers (namespcae Concurrency), but it is just guess;;)
Before you start using the collector and before you create the threads make sure that you issue both
GC_INIT, and
GC_allow_register_threads
Then in every thread follow it up with,
GC_get_stack_base/GC_register_my_thread, and eventually
GC_unregister_my_thread.
You didn't say what you are compiling with but it works for gcc 4.8 (with -std=c++11).
EDIT: The OP was able to resolve the issue by addressing the instruction above and compiling the code with the /MD[d] flags for the multi-threaded dynamic MSVCR100 runtime. The issue remained unresolved for the multithreaded statically compiled runtime.

c++11 Async seg fault

I'm just trying a few of the new C++11 features with GCC 4.7.2, though when I go to run a seg fault occurs.
$ ./a.out
Message from main.
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Aborted (core dumped)
I compiled with the 'beta' features of GCC, in regards to c++0x with:
g++ -std=c++11 c11.cpp
The code:
#include <future>
#include <iostream>
void called_from_async() {
std::cout << "Async call" << std::endl;
}
int main() {
//called_from_async launched in a separate thread if possible
std::future<void> result( std::async(called_from_async));
std::cout << "Message from main." << std::endl;
//ensure that called_from_async is launched synchronously
//if it wasn't already launched
result.get();
return 0;
}
I believe this happens because you have forgot to link with POSIX threads library. Just add -pthread or -lpthread to the g++ flags and the problem should go away.
If you are interested in details, this happens because C++11 runtime is resolving symbols from pthread in run-time only if you happen to use those features. So if you forgot to link, the runtime won't be able to resolve those symbols, treat your environment as if it doesn't support threads, and throw exception (which you don't catch and it aborts your application).

BOOST_THROW_EXCEPTION causing Abort Trap

I'm trying to use boost::exception and have condensed my prototype code down to the example in the Boost exception tutorial however when running the code with the BOOST_THROW_EXCEPTION macro I get an abort from the program.
#include <iostream>
#include <boost/exception/all.hpp>
typedef boost::error_info<struct tag_my_info,int> my_info;
struct my_error: virtual boost::exception, virtual std::exception { };
void f() {
BOOST_THROW_EXCEPTION(my_error() << my_info(42));
// Uncomment the below (and comment the above) for the program to work
//throw my_error() << my_info(42);
}
int main(int argc, char** argv) {
try {
f();
}
catch(my_error& x) {
if(int const* mi = boost::get_error_info<my_info>(x)) {
std::cout << "My info: " << *mi << std::endl;
}
}
return 0;
}
Running the code with the BOOST_THROW_EXCEPTION macro:
$ ./a.out
Abort trap
If as the comment says, I swap the code, all is well
$ ./a.out
My info: 42
Below is the output from the g++ preprocessor for f()
void f() {
::boost::exception_detail::throw_exception_(my_error() << my_info(42),__PRETTY_FUNCTION__,"main.cpp",14);
}
Software versions are:
$ g++ -v
Using built-in specs.
Target: x86_64-apple-darwin10
Thread model: posix
gcc version 4.4.6 (GCC)
$ port list boost
boost #1.47.0 devel/boost
I'm on OSX SL using the tools provided by MacPorts. I've double checked the g++ search paths and there's only one copy of the boost hpp files and that's the ones that belong to the aforementioned boost package.
I have no idea why the abort trap is being called. I admit I'm newish to C++ .
The problem was caused by using the MacPorts version of g++. There are plenty of tickets related to exceptions and Abort Traps in the MP system (and plenty of examples on Google).
Using the version of g++ that comes with XCode enabled this problem to go away.