C++11:多线程编程

进程是正在运行的程序
线程就是进程中的进程

线程库基本用法

创建线程

在这里插入图片描述
但是运行时发现并没有打印helloworld,因为主线程运行完了,子线程还没有创建好,所以需要等待子线程运行完,主线程再退出。

在这里插入图片描述
在这里插入图片描述
join是阻塞的,会阻塞主线程的运行

给线程传递参数

直接在thread的函数后面,有点像绑定器

在这里插入图片描述
在这里插入图片描述

线程分离

主线程不用等待子线程结束,可以先结束。
在这里插入图片描述

常见数据未定义错误

这段会报错
在这里插入图片描述
而且也不能传a在这里插入图片描述
因为线程函数传参是值传递,即使在函数里面用了引用,线程函数拿到的还是复制该引用指向的数据值类型。而且不能把右值传递给左值引用。

在这里插入图片描述
所以传递的左右值引用要统一在这里插入图片描述
确实不加引用的话,能运行在这里插入图片描述

传递指针或引用指向局部变量的问题

这边int a是不能放在函数里面的,因为在函数内部a是局部的,当出了test后,a就自动销毁了,地址也没了,所以就引用不到。要把a设为全局变量。
在这里插入图片描述
C++11的thread默认会复制传递的参数,因此直接传递引用会导致引用被复制,无法修改原始变量。

传递指针或引用指向已释放的内存的问题

ptr是指向1的一个指针,按道理应该是输出1,但是输出了0,说明这个程序已经是错了
在这里插入图片描述
这个错误的原因是,在子线程去打印这个1的时候,主线程可能已经完成释放指针了,那么指针指向的内容会是任何数(野指针)
改进方法,可以主线程加个sleep再释放。
主线程和子线程各跑各的,子程序想要访问某个地址,某块内存,但是主线程已经释放掉了。
在这里插入图片描述
那除了sleep还有什么方法

类成员函数作为入口函数,类对象被提前释放

和上面一样,成员函数在子线程运行,想要访问某个资源的时候,被主线程释放掉了,就取不到资源了
std::thread t(&MyClass::func,&obj)

智能指针来解决该问题

在解决类的释放问题的时候,肯定不能用sleep,因为你也不知道程序要跑多少秒,我们希望程序能够自动地在调用完类对象之后,自动销毁类对象。
那么可以用智能指针来创建对象。这样就不用手写delete,然后又有类对象被提前释放,或者说忘记写delete导致内存泄露的问题。

#include <iostream>
#include <thread>
#include <memory>

std::thread t;

class A{
public:
    void foo(){

        std::cout<<"Hello"<<std::endl;
    }
};



int main()
{
    std::shared_ptr<A> a=std::make_shared<A>();

    std::thread t(&A::foo,a);
    t.join();

    return 0;
}

在这里插入图片描述
为什么这里传入a不需要加引用了,因为a本身就是一个指针了

入口函数为类的私有成员函数

当foo为私有成员函数时,在类外部使用类作用域是调用不到的。
如何解决?
在这里插入图片描述

使用友元
加一个声明就可以了
在这里插入图片描述

互斥量

解决数据共享产生的问题
预期a加的结果为2000000,但是没有到,因为两个线程同时去拿,本应该加2的可能就只能加1了。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在写操作之前对他加锁,写完了之后解锁
如果多线程程序每一次的运行结果和单线程运行的结果始终是一样的,那么你的线程就是安全的。

死锁

一直在运行等待,出现死锁,因为t1需要m2的锁,t2需要m1的锁
在这里插入图片描述
改变顺序就可以了
互斥量多的时候很容易产生死锁

lock_guard与unique_lock

加完锁一定要解锁,这是很严重的一个错误
lock_guard,当构造函数被调用时,该互斥量自动锁定,析构函数被调用时,该互斥量自动解锁。lock_guard对象不能复制或移动,只能在局部作用域中使用。
lock_guard就不用使用加锁和解锁操作
为这个对象传入一个锁类型
在这里插入图片描述
lock_guard源码中,就是构造函数和析构函数自动加锁和解锁

在这里插入图片描述

unique_lock

延迟加锁、条件变量、超时等
unique_lock在lock_guard基础上多了一个延迟加锁的功能
如果5s之后还没有加锁,那么就退出
在这里插入图片描述
但是这样是报错的,因为给对象传入的类型需要是时间锁

在这里插入图片描述
2s内没拿到锁那就直接结束返回了。
正常的加锁是拿不到锁一直等,所以有死锁的情况,那么我这里超时了直接返回。
正常情况是返回4
在这里插入图片描述

std::call_once(单例模式)

单例设计模式确保某个类只能创建一个实例,单例实例是全局唯一的,因此在多线程环境中使用单例模式时,需要考虑线程安全的问题。
比如说日志类,全局只需要一个日志对象,就可以完成所有的打印操作了
单例模式就是要将构造函数私有化,这样才能阻止外部直接创建类对象。
懒汉模式,就是一个懒汉只有在需要的时候才起床一样,只有需要的时候才实例化,饿汉模式,就像饿汉遇到食物一样急不可耐,类加载完后就完成了对象就创建完成了。

在这里插入图片描述
为什么要使用call_once?
当多线程进来之后,单例就调用了两次,违反原则了
在这里插入图片描述
call_once能够确保某个函数只会被调用一次
静态成员函数没有this指针这个形参,所以无法操作非静态成员。
call_once调用函数,需要一个函数,一个onceflag

condition_variable(生产者与消费者模型)

生产者不停去安排任务,消费者不停地去取任务,消费者有很多个(也可以理解为,生产者为一个老板,消费者为很多个打工人)。
条件变量需要和互斥锁一起使用,

#include <iostream>
#include <thread>
#include <memory>
#include <mutex>
#include <windows.h>
#include <string>
#include <condition_variable>
#include <queue>

std::queue<int>g_queue;
std::condition_variable g_cv;
std::mutex mtx;
void Producer(){
    for(int i=0;i<10;i++){
        {
            std::unique_lock<std::mutex> lock(mtx);
            g_queue.push(i);
            //通知消费者来取任务
            g_cv.notify_one();
            std::cout<<"task:"<<i<<std::endl;
        }
        std::this_thread::sleep_for(std::chrono::microseconds(100));

    }
}

void Consumer(){
    while(1){
        std::unique_lock<std::mutex> lock(mtx);
        bool isempty=g_queue.empty();
        //如果队列为空,就要等待
        g_cv.wait(lock,[](){return !g_queue.empty();});
        int value=g_queue.front();
        g_queue.pop();
        std::cout<<"consumer"<<value<<std::endl;
    }
}


int main()
{
    std::thread t1(Producer);
    std::thread t2(Consumer);
    t1.join();
    t2.join();
    return 0;
}

在这里插入图片描述

C++11实现跨平台线程池

#include <iostream>
#include <thread>
#include <memory>
#include <mutex>
#include <windows.h>
#include <string>
#include <condition_variable>
#include <queue>
#include <atomic>
#include <future>
#include <functional>
using namespace std;

class ThreadPool{
public: 
    ThreadPool(int numThreads) :stop(false){
        for(int i=0;i<numThreads;i++){
            threads.emplace_back([this]{
                while(1){
                    std::unique_lock<std::mutex> lock(mtx);
                    condition.wait(lock,[this]{
                        return !tasks.empty()||stop;
                    });
                    if(stop && tasks.empty()){
                        return;
                    }
                    std::function<void()> task(std::move(tasks.front()));
                    tasks.pop();
                    lock.unlock();
                    task();
                }
            });
        }
    }

    ~ThreadPool(){
        {
            std::unique_lock<std::mutex> lock(mtx);
            stop=true;
        }
        condition.notify_all();
        for(auto &t:threads){
            t.join();
        }
        
    }
    template<class F,class... Args>
    void enqueue(F &&f,Args&&... args){
        std::function<void()>task=std::bind(std::forward<F>(f),std::forward<Args>(args)...);
        {
            std::unique_lock<std::mutex> lock(mtx);
            tasks.emplace(std::move(task));
        }
        
        condition.notify_one();
    }
private:
    std::vector<std::thread> threads;
    std::queue<std::function<void()>> tasks;

    std::mutex mtx;
    std::condition_variable condition;

    bool stop;
};

int main(){
    ThreadPool pool(4);

    for(int i=0;i<10;i++){
        pool.enqueue([i]{
            std::cout<<"task:"<<i<<"is running"<<std::endl;
            std::this_thread::sleep_for(std::chrono::seconds(1));
            std::cout<<"task:"<<i<<"is done"<<std::endl;
        });
    }
}

在这里插入图片描述
在这里插入图片描述

为什么task()前面先提前解锁了

task()任务运行过程中不需要占有锁,不然还要等待运行完了再解锁浪费时间
在这里插入图片描述
如果不手动解锁,执行task()的时候还会占有锁,会影响其他线程。这里的锁只需要保护tasks这个任务队列的修改就行了,不需要保护task的执行。

何时调用析构函数

ThreadPool 类的析构函数会在 main 函数结束时被调用。具体来说,析构函数会在 pool 对象的生命周期结束时执行
在这里插入图片描述

异步开发

future

#include <iostream>
#include <thread>
#include <memory>
#include <mutex>
#include <windows.h>
#include <string>
#include <condition_variable>
#include <queue>
#include <atomic>
#include <future>
using namespace std;

int func(){
    int i=0;
    for(i=0;i<1000;i++)
    {
        i++;
    }
    return i;
}

int main()
{
    std::future<int> future_result=std::async(std::launch::async,func);
    cout<<func()<<endl;

    cout<<future_result.get()<<endl;
    return 0;
}


有点相当于又开了个线程去执行函数,然后结果保存在future_result里面。
只不过这个线程不需要自己手动去创建
在这里插入图片描述

packaged_task

task只是把函数封装到task里面,还是需要创建一个线程来跑他的。
而这里task作为一个对象传给线程,要用move,从左值转换到右值。
在这里插入图片描述
在这里插入图片描述

promise

在一个线程中产生一个值,并在另一个线程中获取这个值。通常与future和async一起使用

#include <iostream>
#include <thread>
#include <memory>
#include <mutex>
#include <windows.h>
#include <string>
#include <condition_variable>
#include <queue>
#include <atomic>
#include <future>
using namespace std;

void func(std::promise<int> f){
    f.set_value(1000);
}

int main()
{   
    std::promise<int> f;
    auto future_result=f.get_future();

    std::thread t1(func,std::move(f));

    t1.join();
    cout<<future_result.get()<<endl;
    return 0;
}


在这里插入图片描述
或者用引用传入
在这里插入图片描述
get()
也会阻塞线程,直到promise的执行完毕

原子操作

std::atmoic

除了用互斥量来解决死锁问题,原子操作也能保证线程安全。
直接把shared_data变成一个原子变量,这个与加锁效果是一样的
在这里插入图片描述
在这里插入图片描述
原子操作的运行时间
在这里插入图片描述
而加锁操作的运行时间
在这里插入图片描述
原子操作比解锁,效率更高。

load()

其实就是输出这个值
在这里插入图片描述

store()

对原子变量进行赋值
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海绵波波107

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值