记:Ubuntu visual studio code 使用Boost库

记:Ubuntu visual studio code 使用Boost库

  • 一、Unbuntu 安装Boost库
    • 二、配置visual studio code

学习C++使用多线程开发过程中,需要使用到Boost的共享锁(boost::shared_mutex),不得不配置一下Boost。

一、Unbuntu 安装Boost库

  • 1、查询boost
//进入终端,输入: 
apt-cache search boost; 
  • 2、安装boost
//在显示的众多选项中找到 libboost-all-dev:
 apt-get install libboost-all-dev; 

二、配置visual studio code

  • 1、多线程使用举例
//使用举例:
#include <iostream>
#include <thread>
#include <mutex>
#include <boost/thread/shared_mutex.hpp>
#include <list>

class DNS{
public:
    DNS(){
        std::cout << "constructor..." << std::endl;
    }
    ~DNS() {}
    void DNSread()const{
        if (!dns.empty()) {
            boost::shared_lock<boost::shared_mutex> r_smute(shared_m);
            std::cout << dns.front() << std::endl;
        }
        else
            std::cout << "error: dns is empty..." << std::endl;
    }
    void DNSwrite(int i){
        std::lock_guard<boost::shared_mutex> lg_m(shared_m);
        dns.push_front(i);
        std::cout << "write: " << i << std::endl;
    }
private:
    mutable boost::shared_mutex shared_m;
    std::list<int> dns;    
};

int main()
{
    std::cout << "main: " << std::this_thread::get_id() << std::endl;
    DNS dns;
    std::thread th1(&DNS::DNSwrite, &dns, (int)10);
    std::thread th2(&DNS::DNSread, &dns);
    std::thread th3(&DNS::DNSread, &dns);
    std::thread th4(&DNS::DNSread, &dns);
    th1.join();
    th2.join();
    th3.join();
    th4.join();
    std::cout << "main ok..." << std::endl;
    return 0;
} 
  • 2、配置编译launch.json和task.json

    对于launch.json选取默认的就行

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++-7 build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++-7 build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

对于task.json 使用STL中的多线程需要在编译末尾(argc) 增加:

"-lpthread",

使用Boost中的多线程需要添加:

"-lboost_thread",
"-lboost_system"

task.json

{
// 有关 tasks.json 格式的文档,请参见
    // https://go.microsoft.com/fwlink/?LinkId=733558
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++-7 build active file",
            "command": "/usr/bin/g++-7",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-lpthread",
                "-lboost_thread",
                "-lboost_system"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

你可能感兴趣的:(Ubuntu)