This question already has answers here:
Why does this simple std::thread example not work?
(5 answers)
Closed 7 years ago.
My programs looks like below
#include <iostream>
#include <thread>
#include <exception>
void hello()
{
std::cout << "Hello world!!!!" << std::endl;
}
int main()
{
std::cout << "In Main\n";
std::thread t(hello);
t.join();
return 0;
}
When I compile it using the following command I get no errors
g++-4.7 -std=c++11 main.cpp
But when I run it I get the following error
In Main
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted (core dumped)
Could someone help me with where I am going wrong?
When I use C++11 threads with GCC, i use:
g++ -std=c++0x -pthread -g main.cpp
That works for me.
When compiling your code with g++, use the -pthread option.
Below is the answer I find from stackoverflow:
In g++ is C++ 11 thread model using pthreads in the background?
Everybody already answered that you need the -pthread argument passed to the compiler. Almost for sure it won't change in 4.8 but according to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52681 the exception will at least have a nice message stating what's wrong.
You might need to link against the pthread library
You can try with,
g++ --std=c++11 -lpthread main.cpp
Related
This question already has an answer here:
terminate called after throwing an instance of 'std::system_error'
(1 answer)
Closed 2 years ago.
I use std::call_once in my code, it compiled succeed but crashed when runing...
like this demo:
#include <iostream>
#include <mutex>
using namespace std;
int main()
{
cout << "Hello world" << endl;
static once_flag of;
call_once(of,[]{});
cout << "see you again" << endl;
return 0;
}
Later I found,if i compiled with -static,it crashed,but run succeed just with -pthread or -lpthread:
std::call_once is implemented in libstdc++ on Linux using pthread_once. So you have to link with the pthread library to get its definition. See this thread for details. That's why -pthread becomes necessary. You can also see that using nm or objdump.
To be able to statically link pthread library, you need to ensure the all objects are linked in your binary. See this thread for details.
Instead, you can include the whole archive with:
g++ std_callonce.cpp -g -static -Wl,--whole-archive -lpthread -Wl,--no-whole-archive
That should work as you want.
But note that, though there are some rare cases where static linking is useful, it's generally considered harmful.
I was trying to compile a cpp program for a school project. It uses libbgi for graphics, which works for 32-bit target. For this project i wanted to add some multithreading with std::thread, but the compiler say i couldn't find std::thread.
I tried a compiler with a lower version, with c++11 support.
#include <iostream>
#include <thread>
int main(){
std::thread t([](){
std::cout << "Hello\n";
}
);
t.join();
}
this program compiles fine with -m64, but not with -m32.
I believe you might need to link with pthread and use c++0x.
g++ -std=c++0x -m32 main.cpp -pthread -o main
I recreated your error and it was solved using these flags.
I'm experiencing a bit of strange behaviour when I try to compile any program that uses multithreading in CodeLite.
I get the error:
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not premitted.
after some quick googling I found out I had to add "-pthread" to the compiler options.
Note: that CodeLite puts -l in front of libraries, so it does use -lpthread
After I clean, and rebuild the project, I still get the error though. As far as I can tell the build log looks fine?
The truly frustrating part comes about when I compile it manually via the command line, It works just fine.
I've searched, but none of the solutions seem to work for me? perhaps I've missed a step somewhere?
here's my test code.
I should also note I'm using Ubuntu14.04, and CodeLite 9.1.0
#include <iostream>
#include <string>
#include <thread>
void test()
{
std::cout << " Look it works! \n";
}
void int main(int argc, char** argv)
{
std::thread thrd_1 = std::thread(test);
thrd_1.join();
return 0;
}
Any help would be greatly appreciated!
You are passing -pthread in the compiler options. You need to pass it
in the linker options, and in the linker options you do not need
to specify pthread as a library. The -pthread option means
do whatever it is that links the posix threads library on this platform.
$ g++ -c -O0 -std=c++11 -o main.o main.cpp
$ g++ -o threadtest -pthread main.o
$ ./threadtest
Look it works!
This question already has answers here:
What are the correct link options to use std::thread in GCC under linux?
(5 answers)
Closed 8 years ago.
This code:
#include <iostream>
#include <future>
int main()
{
std::async([]{std::cout << "hello\n";}).get();
}
runs fine and prints hello with Visual C++ 2013, but throws an exception with gcc producing:
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
and doesn't even build with clang, producing this error message:
/usr/bin/ld: /tmp/source-83f28e.o: undefined reference to symbol 'pthread_setspecific##GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I used rexter to run the tests. Can you explain this behavior?
EDIT
With -pthread compiler option gcc version runs fine and clang version builds now and produces:
hello
exception_ptr not yet implemented
In order you add support for multithreading, you need to link against the pthread library. This is -pthread (available as a built-in flag) on both GCC and Clang. Alternatively, -lpthread may do the trick.
My problem is actually described here: Compiling multithread code with g++.
But the answer regarding the work around by using "-Wl,--no-as-needed" is not working for me.
I've added -Wl,--no-as-needed -pthread -std=c++0x in different orders also, but I still get the:
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted"
What to do?
Info:
Ubuntu 12.04LTS
Running Eclipse CDT
g++ v4.8.1
Edit:
I tried building with -Wl,--no-as-needed -lpthread -std=c++0x with no luck.
The code:
#include <iostream>
#include <chrono>
#include <thread>
void foo()
{
std::cout << "Thread 1 created.." << std::endl;
}
int main()
{
std::thread t1(foo);
t1.join();
return 0;
}
Edit:
So unfortunately none of your suggestions worked. I decided to use Boost instead.
it's -Wl,--no-as-needed not -Wl,--no_as_needed, you use the hyphen
-pthread is a flag for the compiler, not the linker, the right one for the linker is -lpthread
Mingw doesn't always comes with the same threading library, there are more than 1 options for multithreading with MinGW, you should document yourself about this according to your MinGW build
g++ filename.c -std=c++11 -lpthread
i am compiling your code with above command its working perfect.