C++ 线程同步操作的几种方法 【LeetCode 1115. 交替打印FooBar】

1115. 交替打印FooBar

原程序如下,我们需要基于原程序进行修改:

class FooBar {
private:
    int n;

public:
    FooBar(int n) {
        this->n = n;
    }

    void foo(function<void()> printFoo) {
        
        for (int i = 0; i < n; i++) {
            
        	// printFoo() outputs "foo". Do not change or remove this line.
        	printFoo();
        }
    }

    void bar(function<void()> printBar) {
        
        for (int i = 0; i < n; i++) {
            
        	// printBar() outputs "bar". Do not change or remove this line.
        	printBar();
        }
    }
};

方法一:使用信号量

#include <semaphore.h>
class FooBar {
private:
    int n;
    sem_t cfoo;
    sem_t cbar;
public:
    FooBar(int n) {
        this->n = n;
        sem_init(&cfoo, 0, 0);
        sem_init(&cbar, 0, 1);
    }

    void foo(function<void()> printFoo) {
        
        for (int i = 0; i < n; i++) {
        	sem_wait(&cbar);
        	printFoo();
            sem_post(&cfoo);
        }
    }

    void bar(function<void()> printBar) {
        
        for (int i = 0; i < n; i++) {
            sem_wait(&cfoo);
        	printBar();
            sem_post(&cbar);
        }
    }
};

方法二:使用互斥锁

class FooBar {
private:
    int n;
    mutex cfoo;
    mutex cbar;
public:
    FooBar(int n) {
        this->n = n;
        cbar.lock();
    }

    void foo(function<void()> printFoo) {
        
        for (int i = 0; i < n; i++) {
        	cfoo.lock();
        	printFoo();
            cbar.unlock();
        }
    }

    void bar(function<void()> printBar) {
        
        for (int i = 0; i < n; i++) {
            cbar.lock();
        	printBar();
            cfoo.unlock();
        }
    }
};

方法三:使用原子变量

class FooBar {
private:
    int n;
    atomic<bool> flag = true;
public:
    FooBar(int n) {
        this->n = n;
    }

    void foo(function<void()> printFoo) {
        
        for (int i = 0; i < n; i++) {
            while (!flag) this_thread::yield(); // 释放时间片
        	printFoo();
            flag = false;
        }
    }

    void bar(function<void()> printBar) {
        
        for (int i = 0; i < n; i++) {
            while(flag) this_thread::yield();
        	printBar();
            flag = true;
        }
    }
};

类似的LeetCode题目还有【1114. 按序打印】和【1116. 打印零与奇偶数

1114. 按序打印

信号量解法:

#include <semaphore.h>
class Foo {
public:
    Foo() {
        sem_init(&firstJobDone, 0, 0);
        sem_init(&secondJobDone, 0, 0);
    }

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

    void second(function<void()> printSecond) {
        sem_wait(&firstJobDone);
        // printSecond() outputs "second". Do not change or remove this line.
        printSecond();
        sem_post(&secondJobDone);
    }

    void third(function<void()> printThird) {
        sem_wait(&secondJobDone);
        // printThird() outputs "third". Do not change or remove this line.
        printThird();
    }
protected:
    sem_t firstJobDone;
    sem_t secondJobDone;
};

1116. 打印零与奇偶数

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值