You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
403 B
27 lines
403 B
#include <thread>
|
|
#include <semaphore>
|
|
constexpr int loop = 100000;
|
|
std::binary_semaphore a{0}, b{1};
|
|
|
|
void producer() {
|
|
for(int i = 0; i < loop; ++i) {
|
|
a.acquire();
|
|
b.release();
|
|
}
|
|
}
|
|
|
|
void consumer() {
|
|
for(int i = 0; i < loop; ++i) {
|
|
b.acquire();
|
|
a.release();
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
std::thread t1(producer);
|
|
std::thread t2(consumer);
|
|
t1.join();
|
|
t2.join();
|
|
puts("done");
|
|
}
|