3

A simple code with C++11 thread:

#include <iostream>
#include <thread>
#include <string>

using namespace std;
void thread_task(){
    std::cout<<"hello, here is thread task with state:"<<std::endl;
}

int main(int argc, char *argv[])
{
    std::thread t(thread_task);
    t.join();
    return 0;
}

compile:

g++ -pthread -std=c++11 -O0 -Wall -g -ggdb  main.cpp  -o main  -lboost_system -lboost_filesystem -lboost_serialization

./main can execute right!

--

g++ -pthread -std=c++11 -O0 -Wall -g -ggdb  main.cpp  -o main  -lboost_system -lboost_filesystem -lboost_serialization -lboost_container-mt

g++ -pthread -std=c++11 -O0 -Wall -g -ggdb  main.cpp  -o main  -lboost_system -lboost_filesystem -lboost_serialization -lboost_container 

./main all will have an error:
terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
[1]    29643 abort (core dumped)  ./main

why -lboost_container lead to this error? Certainly, boost_container is no need in my code example. It is noly a example.

Anton Savin
  • 40,838
  • 8
  • 54
  • 90
LazyGolden
  • 33
  • 4

1 Answers1

0

this was answeredhere

g++ -Wl,--no-as-needed -pthread -std=c++11 -O0 -Wall -g -ggdb  main.cpp  -o main  -lboost_system -lboost_filesystem -lboost_serialization -lboost_container
JerryYoung
  • 267
  • 1
  • 3