LeetCode(1114)按序打印
线程通信
#include <semaphore.h> class Foo { protected: sem_t firstJobDone; sem_t secondJobDone; public: Foo() { sem_init(&firstJobDone, 0, 0); sem_init(&secondJobDone, 0, 0); } void first(function<void()> printFirst) { // printFirst() outputs "first". printFirst(); sem_post(&firstJobDone); } void second(function<void()> printSecond) { sem_wait(&firstJobDone); // printSecond() outputs "second". printSecond(); sem_post(&secondJobDone); } void third(function<void()> printThird) { sem_wait(&secondJobDone); // printThird() outputs "third". printThird(); } }; 作者:LeetCode 链接:https://leetcode.cn/problems/print-in-order/solution/an-xu-da-yin-by-leetcode/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

浙公网安备 33010602011771号