https://leetcode.com/problems/print-in-order/
코드 :
class Foo {
atomic<int> turn = 1;
public:
Foo() {
}
void first(function<void()> printFirst) {
while (turn <= 1)
if (turn == 1) {
printFirst();
turn++;
}
}
void second(function<void()> printSecond) {
while (turn <= 2)
if (turn == 2) {
printSecond();
turn++;
}
}
void third(function<void()> printThird) {
while (turn <= 3)
if (turn == 3) {
printThird();
turn++;
}
}
};
|
cs |
해설 :
class Foo {
atomic<int> turn = 1; // 쓰레드에도 영향을 받지 않는 원자적 정수를 선언합니다.
public:
Foo() {
}
void first(function<void()> printFirst) {
while (turn <= 1) // 1이 올 때 까지 대기 합니다
if (turn == 1) {
printFirst();
turn++;
}
}
void second(function<void()> printSecond) {
while (turn <= 2) // 2가 올 때 까지 대기합니다
if (turn == 2) {
printSecond();
turn++;
}
}
void third(function<void()> printThird) {
while (turn <= 3) // 3이 올 떄 까지
if (turn == 3) {
printThird();
turn++;
}
}
};
|
cs |
'Leetcode' 카테고리의 다른 글
LeetCode 178. Rank Scores [MySql] (0) | 2022.06.03 |
---|---|
LeetCode 1115. Print FooBar Alternately [C++/ promise] (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 |
Remove Nth Node From End of List, LeetCode, c++/cpp/c (0) | 2022.06.01 |