proxygen
ThreadLocalDestroyBenchmark.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

template<typename Tag1 , typename Tag2 >
void runTestTag (int iters, int numThreads)
 
void runTestSameTag (int iters, int numThreads)
 
void runTestDiffTag (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 119 of file ThreadLocalDestroyBenchmark.cpp.

References folly::runBenchmarks().

119  {
120  gflags::ParseCommandLineFlags(&argc, &argv, true);
122 
123  return 0;
124 }
void runBenchmarks()
Definition: Benchmark.cpp:456
char ** argv
void runTestDiffTag ( int  iters,
int  numThreads 
)

Definition at line 99 of file ThreadLocalDestroyBenchmark.cpp.

References folly::BENCHMARK_DRAW_LINE(), BENCHMARK_PARAM, and runTestSameTag().

99  {
100  runTestTag<void, int>(iters, numThreads);
101 }
void runTestSameTag ( int  iters,
int  numThreads 
)

Definition at line 95 of file ThreadLocalDestroyBenchmark.cpp.

Referenced by runTestDiffTag().

95  {
96  runTestTag<void, void>(iters, numThreads);
97 }
template<typename Tag1 , typename Tag2 >
void runTestTag ( int  iters,
int  numThreads 
)

Definition at line 26 of file ThreadLocalDestroyBenchmark.cpp.

References folly::BenchmarkSuspender::dismiss(), i, m, mutex, folly::BenchmarkSuspender::rehire(), folly::ThreadLocalPtr< T, Tag, AccessMode >::reset(), folly::pushmi::detail::t, and threads.

26  {
27  BenchmarkSuspender susp;
28 
30 
31  t1.reset(new int(1));
32 
33  std::mutex m, mw;
34  std::condition_variable cv, cvw;
35  bool running = true;
36  int numRunning = 0;
37 
38  std::vector<std::thread> threads;
39  for (int i = 0; i < numThreads; i++) {
40  threads.push_back(std::thread([&]() mutable {
41  t1.reset(new int(1));
42 
43  // notify if all the threads have created the t1
44  bool notify = false;
45  {
46  std::lock_guard<std::mutex> lk(m);
47  if (++numRunning == numThreads) {
48  notify = true;
49  }
50  }
51 
52  if (notify) {
53  cv.notify_one();
54  }
55 
56  // now wait
57  {
58  std::unique_lock<std::mutex> lk(mw);
59  cvw.wait(lk, [&]() { return !running; });
60  }
61  }));
62  }
63 
64  // wait for the threads to create the t1
65  {
66  std::unique_lock<std::mutex> lk(m);
67  cv.wait(lk, [&]() { return numRunning == numThreads; });
68  }
69 
71  t3.reset(new int(2));
72 
73  susp.dismiss();
74 
75  // run the test loop
76  for (int i = 0; i < iters; i++) {
78  t2.reset(new int(2));
79  }
80 
81  susp.rehire();
82 
83  {
84  std::lock_guard<std::mutex> lk(mw);
85  running = false;
86  }
87 
88  cvw.notify_all();
89 
90  for (auto& t : threads) {
91  t.join();
92  }
93 }
void reset(T *newPtr=nullptr)
Definition: ThreadLocal.h:176
std::vector< std::thread::id > threads
static map< string, int > m
std::mutex mutex