C++11 线程 boost 容器

标签 c++ multithreading c++11 boost

C++11线程的简单代码:

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

编译:

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

为什么 -lboost_container 会导致这个错误?当然,在我的代码示例中不需要 boost_container。这只是一个例子。

最佳答案

已回答 here

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

关于C++11 线程 boost 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29121925/

相关文章:

c++ - SNMP:无法创建/设置变量,编码错误

c++ - 多线程环境中单例资源的互斥保护

c - 没有阻塞线程

c++ - 如果线程必须写在同一个 vector 上,是否可以使用线程

c++ - 如果存储 unique_ptr,则不会构建 std::queue 的线程安全包装器,但 std::queue 可以工作

c++ - D3DXERR_INVALIDDATA 发生

c++ - 如何初始化 GLfloat 数组?

c++ - 语言环境 "en_US"中 std::time_get::get_time() 的正确时间格式是什么?

c++ - 如何在 Qt 中重新启动一个线程?

c++ - 如何检索模板类的 boost::variant 值