栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 多线程按顺序执行函数

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 多线程按顺序执行函数

我们提供了一个类:

public class Foo {
  public void first() { print("first"); }
  public void second() { print("second"); }
  public void third() { print("third"); }
}

三个不同的线程 A、B、C 将会共用一个 Foo 实例。

一个将会调用 first() 方法
一个将会调用 second() 方法
还有一个将会调用 third() 方法
请设计修改程序,以确保 second() 方法在 first() 方法之后被执行,third() 方法在 second() 方法之后被执行。

法一:信号量
#include 
class Foo {
private:
    sem_t firstDone;
    sem_t secondDone;
public:
    Foo() {
        sem_init(&firstDone,0,0);
        sem_init(&secondDone,0,0);
    }

    void first(function printFirst) {
        
        // printFirst() outputs "first". Do not change or remove this line.
        printFirst();
        sem_post(&firstDone);
    }

    void second(function printSecond) {
        sem_wait(&firstDone);
        // printSecond() outputs "second". Do not change or remove this line.
        printSecond();
        sem_post(&secondDone);
    }

    void third(function printThird) {
        sem_wait(&secondDone);
        // printThird() outputs "third". Do not change or remove this line.
        printThird();
    }
};
互斥锁:mutex
#include 
class Foo {
private:
    mutex mtx1;
    mutex mtx2;
public:
    Foo() {
        mtx1.lock();
        mtx2.lock();
    }

    void first(function printFirst) {
        
        // printFirst() outputs "first". Do not change or remove this line.
        printFirst();
        mtx1.unlock();
    }

    void second(function printSecond) {
        mtx1.lock();
        // printSecond() outputs "second". Do not change or remove this line.
        printSecond();
        mtx1.unlock();
        mtx2.unlock();
    }

    void third(function printThird) {
        mtx2.lock();
        // printThird() outputs "third". Do not change or remove this line.
        printThird();
        mtx2.unlock();
    }
};
RAII lock_guard, unique_lock
#include 
class Foo {
private:
    mutex mtx1;
    mutex mtx2;
    unique_lock m1lock,m2lock;
public:
    Foo() :m1lock(mtx1,try_to_lock),m2lock(mtx2,try_to_lock){
        
    }

    void first(function printFirst) {
        
        // printFirst() outputs "first". Do not change or remove this line.
        printFirst();
        m1lock.unlock();
    }

    void second(function printSecond) {
        lock_guard guard(mtx1);
        // printSecond() outputs "second". Do not change or remove this line.
        printSecond();
        m2lock.unlock();
    }

    void third(function printThird) {
        lock_guard guard(mtx2);
        // printThird() outputs "third". Do not change or remove this line.
        printThird();
    }
};
条件变量
#include 
class Foo {
private:
    condition_variable cv;
    mutex mtx;
    int k;
public:
    Foo() {
        k = 0;
    }

    void first(function printFirst) {
        printFirst();
        k = 1;
        cv.notify_all();
    }

    void second(function printSecond) {
        unique_lock lock(mtx);
        cv.wait(lock,[this](){return k==1;});
        printSecond();
        k=2;
        cv.notify_one();
    }

    void third(function printThird) {
        unique_lock lock(mtx);
        cv.wait(lock,[this](){return k==2;});
        printThird();
    }
};
原子操作 异步操作
class Foo {
    promise pro1,pro2;
public:
    Foo() {
        
    }

    void first(function printFirst) {
        printFirst();
        pro1.set_value();
    }

    void second(function printSecond) {
        pro1.get_future().wait();
        printSecond();
        pro2.set_value();
    }

    void third(function printThird) {
        pro2.get_future().wait();
        printThird();
    }
};
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/291205.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号