Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <thread>
- #include <condition_variable>
- #include <mutex>
- #include <chrono>
- int main() {
- enum { COUNT = 1000000 };
- volatile int ping = 0, pang = 0;
- auto clock = std::chrono::high_resolution_clock::now();
- std::condition_variable cvPing, cvPang;
- std::mutex mtx;
- std::thread thd([&](){
- cvPing.notify_all();
- std::unique_lock<std::mutex> lock(mtx);
- while (pang < COUNT) {
- cvPang.wait(lock);
- ++pang;
- cvPing.notify_all();
- }
- });
- {
- std::unique_lock<std::mutex> lock(mtx);
- while (ping < COUNT) {
- cvPing.wait(lock);
- ++ping;
- cvPang.notify_all();
- }
- }
- thd.join();
- std::printf("PING: %d, PANG: %d, Time used: %lldms\n", ping, pang, (std::chrono::high_resolution_clock::now() - clock).count() / 1000000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement