proxygen
ThreadLocalAccessBenchmark.cpp File Reference
#include <folly/Benchmark.h>
#include <folly/ThreadLocal.h>
#include <condition_variable>
#include <mutex>
#include <thread>

Go to the source code of this file.

Functions

void runTest (int iters, int numThreads)
 
 BENCHMARK_DRAW_LINE ()
 
int main (int argc, char *argv[])
 

Function Documentation

BENCHMARK_DRAW_LINE ( )
int main ( int  argc,
char *  argv[] 
)

Definition at line 123 of file ThreadLocalAccessBenchmark.cpp.

References folly::runBenchmarks().

123  {
124  gflags::ParseCommandLineFlags(&argc, &argv, true);
126 
127  return 0;
128 }
void runBenchmarks()
Definition: Benchmark.cpp:456
char ** argv
void runTest ( int  iters,
int  numThreads 
)

Definition at line 42 of file ThreadLocalAccessBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), BENCHMARK_PARAM, folly::BenchmarkSuspender::dismiss(), i, m, mutex, folly::BenchmarkSuspender::rehire(), folly::pushmi::detail::t, and threads.

42  {
43  BenchmarkSuspender susp;
44 
45  std::vector<SimpleThreadCachedInt> stci(numThreads);
46 
47  std::mutex m, mw;
48  std::condition_variable cv, cvw;
49  bool running = true;
50  int numRunning = 0;
51 
52  std::vector<std::thread> threads;
53  for (int i = 0; i < numThreads; i++) {
54  threads.push_back(std::thread([i,
55  numThreads,
56  &stci,
57  &m,
58  &mw,
59  &cv,
60  &cvw,
61  &running,
62  &numRunning]() mutable {
63  stci[i].set();
64 
65  // notify if all the threads have created the
66  // thread local var
67  bool notify = false;
68  {
69  std::lock_guard<std::mutex> lk(m);
70  if (++numRunning == numThreads) {
71  notify = true;
72  }
73  }
74 
75  if (notify) {
76  cv.notify_one();
77  }
78 
79  // now wait
80  {
81  std::unique_lock<std::mutex> lk(mw);
82  cvw.wait(lk, [&]() { return !running; });
83  }
84  }));
85  }
86 
87  // wait for the threads to create the thread locals
88  {
89  std::unique_lock<std::mutex> lk(m);
90  cv.wait(lk, [&]() { return numRunning == numThreads; });
91  }
92 
93  susp.dismiss();
94 
95  // run the test loop
96  for (int i = 0; i < iters; i++) {
97  stci[i % numThreads].access();
98  }
99 
100  susp.rehire();
101 
102  {
103  std::lock_guard<std::mutex> lk(mw);
104  running = false;
105  }
106 
107  cvw.notify_all();
108 
109  for (auto& t : threads) {
110  t.join();
111  }
112 }
std::vector< std::thread::id > threads
static map< string, int > m
std::mutex mutex