From 2b560f39b2b7a492dde546977d3bcdef1a18d8f8 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 2 Mar 2019 21:46:48 -0500 Subject: [PATCH] use lambda for thread function --- README.md | 53 +++++++++++++++++++---------------------------------- bench.cc | 54 ++++++++++++++++++++---------------------------------- 2 files changed, 39 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 12f7a24..39ba4c3 100644 --- a/README.md +++ b/README.md @@ -123,48 +123,33 @@ void _fill_random_inner(int64_t cnt, HT &hash, RSU &rsu) and here is the code for the multi-threaded insert: ```c++ -// -------------------------------------------------------------------------- -template -struct TD -{ - int64_t thread_idx; // index of this thread (0 to num_threads) - int64_t num_threads; // number of threads inserting values into the hash_map - int64_t cnt; // number of values to insert (10 million in this benchmark) - HT &hash; // the absl::parallel_flat_hash_map - RSU rsu; // generates a random sequence of unique integers -}; - -// -------------------------------------------------------------------------- -template -void _fill_random_inner_thr(TD td) -{ - typename HT::hasher hasher; // get hasher object from the hash table - size_t modulo = td.hash.subcnt() / td.num_threads; // subcnt() returns the number of subtables - - for (int64_t i=0; i void _fill_random_inner_mt(int64_t cnt, HT &hash, RSU &rsu) { - constexpr int64_t num_threads = 8; // has to be a power of two + constexpr int64_t num_threads = 8; // has to be a power of two std::unique_ptr threads[num_threads]; + auto thread_fn = [&hash, cnt, num_threads](int64_t thread_idx, RSU rsu) { + typename HT::hasher hasher; // get hasher object from the hash table + size_t modulo = hash.subcnt() / num_threads; // subcnt() returns the number of subtables + + for (int64_t i=0; i td {i, num_threads, cnt, hash, rsu}; - threads[i].reset(new std::thread(_fill_random_inner_thr, td)); + threads[i].reset(new std::thread(thread_fn, i, rsu)); } // rsu passed by value to threads... we need to increment the reference object diff --git a/bench.cc b/bench.cc index bd4300d..3c4dc78 100644 --- a/bench.cc +++ b/bench.cc @@ -155,39 +155,6 @@ void _fill_random_inner(int64_t cnt, HT &hash, RSU &rsu) } } -// -------------------------------------------------------------------------- -template -struct TD -{ - int64_t thread_idx; - int64_t num_threads; - int64_t cnt; - HT &hash; - RSU rsu; // generates a random sequence of unique integers -}; - -// -------------------------------------------------------------------------- -template -void _fill_random_inner_thr(TD td) -{ -#ifdef MT_SUPPORT - typename HT::hasher hasher; // get hasher object from the hash table - size_t modulo = td.hash.subcnt() / td.num_threads; // subcnt() returns the number of subtables - - for (int64_t i=0; i void _fill_random_inner_mt(int64_t cnt, HT &hash, RSU &rsu) @@ -195,10 +162,29 @@ void _fill_random_inner_mt(int64_t cnt, HT &hash, RSU &rsu) constexpr int64_t num_threads = 8; // has to be a power of two std::unique_ptr threads[num_threads]; + auto thread_fn = [&hash, cnt, num_threads](int64_t thread_idx, RSU rsu) { +#ifdef MT_SUPPORT + typename HT::hasher hasher; // get hasher object from the hash table + size_t modulo = hash.subcnt() / num_threads; // subcnt() returns the number of subtables + + for (int64_t i=0; i td {i, num_threads, cnt, hash, rsu}; - threads[i].reset(new std::thread(_fill_random_inner_thr, td)); + threads[i].reset(new std::thread(thread_fn, i, rsu)); } // rsu passed by value to threads... we need to increment the reference object