https://leetcode.com/problems/print-foobar-alternately/
코드 :
class FooBar {
private:
int n;
std::promise<bool> a, b; // 꼭 출력해줄 것이라 약속한다
public:
FooBar(int n) {
this->n = n;
b.set_value(true); // foo를 먼저 출력해야하므로 b를 먼저 출력을 허가해 준다
}
void foo(function<void()> printFoo) {
for (int i = 0; i < n; i++) {
b.get_future().get(); // b를 받는다
printFoo();
a = promise<bool>();
a.set_value(true); // a를 출력해 줄 것이라 약속한다
}
}
void bar(function<void()> printBar) {
for (int i = 0; i < n; i++) {
a.get_future().get(); // a를 받는다
printBar();
b = promise<bool>();
b.set_value(true); // b를 출력해 줄 것이라 약속한다
}
}
};
|
cs |
'Leetcode' 카테고리의 다른 글
LeetCode 180. Consecutive Numbers [MySql] (0) | 2022.06.03 |
---|---|
LeetCode 178. Rank Scores [MySql] (0) | 2022.06.03 |
LeetCode 1114. Print in Order [C++/ atomic] (0) | 2022.06.03 |
LeetCode 1114. Print in Order [C++/ promise] (0) | 2022.06.03 |
Merge Two Sorted Lists / leetcode / c++ (0) | 2022.06.01 |