我们提供了一个类:

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 <semaphore.h>
class Foo {
private:
sem_t firstDone;
sem_t secondDone;
public:
Foo() {
sem_init(&firstDone,0,0);
sem_init(&secondDone,0,0);
} void first(function<void()> printFirst) { // printFirst() outputs "first". Do not change or remove this line.
printFirst();
sem_post(&firstDone);
} void second(function<void()> printSecond) {
sem_wait(&firstDone);
// printSecond() outputs "second". Do not change or remove this line.
printSecond();
sem_post(&secondDone);
} void third(function<void()> printThird) {
sem_wait(&secondDone);
// printThird() outputs "third". Do not change or remove this line.
printThird();
}
};

互斥锁:mutex

#include <semaphore.h>
class Foo {
private:
mutex mtx1;
mutex mtx2;
public:
Foo() {
mtx1.lock();
mtx2.lock();
} void first(function<void()> printFirst) { // printFirst() outputs "first". Do not change or remove this line.
printFirst();
mtx1.unlock();
} void second(function<void()> printSecond) {
mtx1.lock();
// printSecond() outputs "second". Do not change or remove this line.
printSecond();
mtx1.unlock();
mtx2.unlock();
} void third(function<void()> printThird) {
mtx2.lock();
// printThird() outputs "third". Do not change or remove this line.
printThird();
mtx2.unlock();
}
};

RAII lock_guard, unique_lock

#include <semaphore.h>
class Foo {
private:
mutex mtx1;
mutex mtx2;
unique_lock<mutex> m1lock,m2lock;
public:
Foo() :m1lock(mtx1,try_to_lock),m2lock(mtx2,try_to_lock){ } void first(function<void()> printFirst) { // printFirst() outputs "first". Do not change or remove this line.
printFirst();
m1lock.unlock();
} void second(function<void()> printSecond) {
lock_guard<mutex> guard(mtx1);
// printSecond() outputs "second". Do not change or remove this line.
printSecond();
m2lock.unlock();
} void third(function<void()> printThird) {
lock_guard<mutex> guard(mtx2);
// printThird() outputs "third". Do not change or remove this line.
printThird();
}
};

条件变量

#include <semaphore.h>
class Foo {
private:
condition_variable cv;
mutex mtx;
int k;
public:
Foo() {
k = 0;
} void first(function<void()> printFirst) {
printFirst();
k = 1;
cv.notify_all();
} void second(function<void()> printSecond) {
unique_lock<mutex> lock(mtx);
cv.wait(lock,[this](){return k==1;});
printSecond();
k=2;
cv.notify_one();
} void third(function<void()> printThird) {
unique_lock<mutex> lock(mtx);
cv.wait(lock,[this](){return k==2;});
printThird();
}
};

原子操作

异步操作

class Foo {
promise<void> pro1,pro2;
public:
Foo() { } void first(function<void()> printFirst) {
printFirst();
pro1.set_value();
} void second(function<void()> printSecond) {
pro1.get_future().wait();
printSecond();
pro2.set_value();
} void third(function<void()> printThird) {
pro2.get_future().wait();
printThird();
}
};

博客地址:www.orzlinux.cn

C++ 多线程按顺序执行函数的更多相关文章

  1. ES6 Promise 让异步函数顺序执行

    应用 ES6 的 内置对象 Promise, 让异步函数 按顺序执行的例子 如下: 上边 是四个用Promise 处理过的 异步执行的函数: fn1.fn2.fn3.fn4 下面,让其按顺序执行 如下 ...

  2. Promise 异步函数顺序执行

    可以满足需求,且使用方法和Promise.all统一 var a = function() { return new Promise(function(resolve, reject) { setTi ...

  3. Qt 控制线程的顺序执行(使用QWaitCondition,并且线程类的run函数里记得加exec(),使得线程常驻)

    背景项目中用到多线程,对线程的执行顺序有要求: A.一个线程先收数据 B.一个线程处理数据 C.一个线程再将处理后的数据发送出去 要求三个线程按照ABC的顺序循环执行. 思路子类化多线程方法 重写子类 ...

  4. .NET多线程执行函数

    前面几篇文章一直在写LINQ,这里为什么会出现多线程?原因是DebugLZQ在写一个LINQ综合Demo的时候遇到了多线程,便停下手来整理一下.关于多线程的文章,园子里很多很多,因此关于多线程理论性的 ...

  5. js函数整合队列顺序执行插件

    前言 在日常开发中,也许我们会遇到这样的一个问题.我们利用[发布订阅模式](如果不了解的可以直接访问此链接www.cnblogs.com/xiaoxiaokun- )去执行[发布]事件时,遇到函数内部 ...

  6. 更优雅的方式: JavaScript 中顺序执行异步函数

    火于异步 1995年,当时最流行的浏览器--网景中开始运行 JavaScript (最初称为 LiveScript). 1996年,微软发布了 JScript 兼容 JavaScript.随着网景.微 ...

  7. UI基础:UI程序执行顺序(UIApplicationMain()函数),自定义视图 分类: iOS学习-UI 2015-07-02 22:09 68人阅读 评论(0) 收藏

    UI程序的一般执行顺序: 先进入main里面,执行函数UIApplicationMain(),通过该函数创建应用程序对象和指定其代理并实现监听,当执行函数UIApplicationMain()时还会做 ...

  8. C# 不使用Task实现的多线程顺序执行

    多线程有很好的并发性即无序性,在某些特殊情况下需要用到多线程然而又要使其具备顺序性,这种时候就有了一个特殊的场景那就是多线程顺序执行,在现在VS2015中Task自带了顺序执行的方法,但在此之前的旧项 ...

  9. [Thread] 多线程顺序执行

    Join 主线程join 启动线程t1,随后调用join,main线程需要等t1线程执行完毕后继续执行. public class MainJoin { static class MyThread i ...

随机推荐

  1. WPF 中TextBox 增加输入检测,错误提示

    先来总结下实现错误提示功能的几个要点 1:binding 的ValidationRules 2 :Validation.ErrorTemplate 首先我们在界面添加一个TextBox, Text绑定 ...

  2. 十九:JDBC操作事务

    二.MySQL数据库中操作事务命令 2.1.开启事务(start transaction) 使用"start transaction"开启MySQL数据库的事务,如下所示:

  3. Quartz任务调度(1)概念例析快速

    实例解析概念 在quartz中,有几个核心类和接口:Job.JobDetail.Trigger.Calendar.Scheduler.下面我们结合实例来分析这些类的角色定位.现在我们有一个新闻网站,它 ...

  4. Springboot使用MatrixVariable 注解

        根据 URI 规范 RFC 3986 中 URL 的定义,路径片段中可以可以包含键值对.规范中没对对应的术语.一般 "URL 路径参数" 可以被应用,尽管更加独特的 &qu ...

  5. JavaWeb之JavaMail

    时间:2016-12-19 11:58 --邮件协议1.收发邮件    发邮件是从客户端把邮件发送到服务器,收邮件是把邮件服务器的邮件下载到客户端    2.邮件协议概述    与HTTP协议相同, ...

  6. ubuntu中用update-alternatives进行软件多版本设置、切换,以python配置为例

    以Python2.7和Python3.5设置为例: 在系统中添加Python2.7.Python3.5的选项,默认为Python3.5 sudo update-alternatives --insta ...

  7. Learning ROS: Running ROS across multiple machines

    Start the master ssh hal roscore Start the listener ssh hal export ROS_MASTER_URI=http://hal:11311 r ...

  8. 四种cmd打开方式

    四种cmd打开方式: 开始+系统+命令提示符: Win+R 输入cmd回车(推荐使用): 按住shift键加右键,点击打开PowerShell窗口: 资源管理器的地址栏前加入cmd+空格

  9. ASH数据的迁移:导出导入

    自己写的小工具: 查看帮助 [oracle@redhat76 2]$ ./orash Usage: sh orash keyword [value1] [value2] --------------- ...

  10. 《通过刷leetcode学习Go语言》之(1):序言

    Author       : Email         : vip_13031075266@163.com Date          : 2021.03.07 Version     : 北京 C ...