What is the best way to share objects between threads?

Skip to first unread message


Jun 3, 2020, 11:58:04 AM6/3/20
to benchmark-discuss
What I want to achieve is a object, which is shared among the threads of a benchmark and whose life time starts, when the benchmark starts and ends, when the benchmark is finished.
Currently I use a fixture and create the object in the constructor (Setup and Teardown is called too often). However this creates the object before the program's main, which can be the first problem. I can avoid that problem by moving BENCHMARK_REGISTER_F to the main. Still the created object is unconfigurable by command line arguments. RegisterBenchmark could help here.
However I fail to see a way to destroy the object at the proper time even when using RegisterBenchmark. If I run a lot of benchmarks, I might leak too much memory.

I'd like to see an enhanced RunBenchmark:

RunResults RunBenchmark(
    const benchmark::internal::BenchmarkInstance& b,
    std::vector<BenchmarkReporter::Run>* complexity_reports) {
  internal::BenchmarkRunner r(b, complexity_reports);
  return r.get_results();

StartRunning and FinishRunning are newly introduced virtual functions, which would solve my problem, since the benchmark object itself is shared.
Are there other ways to control the lifetime of shared objects?

Dominic Hamon

Jun 3, 2020, 12:26:50 PM6/3/20
to olafkr...@googlemail.com, benchmark-discuss
Something like https://github.com/google/benchmark#multithreaded-benchmarks ?

I may have misunderstood your question.

You received this message because you are subscribed to the Google Groups "benchmark-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to benchmark-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/benchmark-discuss/86141012-e12b-4605-9fe9-7ea32ab43a94%40googlegroups.com.

Olaf Krzikalla

Jun 3, 2020, 1:58:11 PM6/3/20
to Dominic Hamon, benchmark-discuss
No, that's not what I am looking for. The linked example is unable to create a shared object. I want
(a) spawn the threads before the measurement, so that I don't measure the spawning. Using the google-benchmark machinery for that is fine.
(b) a shared object, whose lifetime I can control.

I am afraid that there is no nice solution yet.
Reply all
Reply to author
0 new messages