Skip to content

Instantly share code, notes, and snippets.

@alexeiz
Last active April 28, 2016 04:01
Show Gist options
  • Save alexeiz/f2a513295c0a47318bcd9849f50d9e05 to your computer and use it in GitHub Desktop.
Save alexeiz/f2a513295c0a47318bcd9849f50d9e05 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <boost/thread/barrier.hpp>
using namespace std;
using clock_type = chrono::system_clock;
mutex mtx;
condition_variable cond;
boost::barrier bar{2};
clock_type::time_point before_notify;
clock_type::time_point after_wait;
void thrproc()
{
unique_lock<mutex> lock{mtx};
bar.count_down_and_wait();
cond.wait(lock);
after_wait = clock_type::now();
}
int main()
{
auto thr = thread{thrproc};
bar.count_down_and_wait();
{
lock_guard<mutex> lock{mtx};
before_notify = clock_type::now();
cond.notify_all();
}
thr.join();
cout << chrono::duration_cast<chrono::microseconds>(after_wait - before_notify).count() << endl;
}
/*
% g++ --version
g++ (GCC) 5.3.1 20160406 (Red Hat 5.3.1-6)
% g++ -std=c++14 -O3 condition-perf.cpp -pthread -lboost_system -lboost_thread
CPU: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
Run iterations: 10000
sum:142385
avg:14.2385
std:10.4202791589285
med:14
max:76
min:2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment