leetcode & c++多线程刷题日志

1|01.按序打印

按序打印
解法

    1. 互斥锁
class Foo { mutex mtx1, mtx2; public: Foo() { mtx1.lock(), mtx2.lock(); } void first(function<void()> printFirst) { printFirst(); mtx1.unlock(); } void second(function<void()> printSecond) { mtx1.lock(); printSecond(); mtx1.unlock(); mtx2.unlock(); } void third(function<void()> printThird) { mtx2.lock(); printThird(); mtx2.unlock(); } };
    1. 条件变量
class Foo { condition_variable cv; mutex mtx; int k = 0; public: void first(function<void()> printFirst) { printFirst(); k = 1; cv.notify_all(); // 通知其他所有在等待唤醒队列中的线程 } void second(function<void()> printSecond) { unique_lock<mutex> lock(mtx); // lock mtx cv.wait(lock, [this](){ return k == 1; }); // unlock mtx,并阻塞等待唤醒通知,需要满足 k == 1 才能继续运行 printSecond(); k = 2; cv.notify_one(); // 随机通知一个(unspecified)在等待唤醒队列中的线程 } void third(function<void()> printThird) { unique_lock<mutex> lock(mtx); // lock mtx cv.wait(lock, [this](){ return k == 2; }); // unlock mtx,并阻塞等待唤醒通知,需要满足 k == 2 才能继续运行 printThird(); } };
    1. <sempahore.h>
class Foo { private: sem_t sem_1, sem_2; public: Foo() { sem_init(&sem_1, 0, 0), sem_init(&sem_2, 0, 0); } void first(function<void()> printFirst) { printFirst(); sem_post(&sem_1); } void second(function<void()> printSecond) { sem_wait(&sem_1); printSecond(); sem_post(&sem_2); } void third(function<void()> printThird) { sem_wait(&sem_2); printThird(); } };
    1. std::future 异步操作
class Foo { promise<void> pro1, pro2; public: 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(); } };
  • 原子操作
class Foo { std::atomic<bool> a{ false }; std::atomic<bool> b{ false }; public: void first(function<void()> printFirst) { printFirst(); a = true; } void second(function<void()> printSecond) { while (!a) this_thread::sleep_for(chrono::milliseconds(1)); printSecond(); b = true; } void third(function<void()> printThird) { while (!b) this_thread::sleep_for(chrono::milliseconds(1)); printThird(); } };

2|02 交替打印 FooBar

交替打印 FooBar
解法

  • <semaphore.h>
#include<semaphore.h> class FooBar { private: int n; sem_t m1, m2; public: FooBar(int n) { this->n = n; sem_init(&m1, 0, 0); sem_init(&m2, 0, 0); sem_post(&m2); } void foo(function<void()> printFoo) { for (int i = 0; i < n; i++) { sem_wait(&m2); // printFoo() outputs "foo". Do not change or remove this line. printFoo(); sem_post(&m1); } } void bar(function<void()> printBar) { for (int i = 0; i < n; i++) { sem_wait(&m1); // printBar() outputs "bar". Do not change or remove this line. printBar(); sem_post(&m2); } } };

__EOF__

本文作者InsiApple
本文链接https://www.cnblogs.com/InsiApple/p/17665747.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   InsiApple  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示