Commit 373cc411 by Matt Clarkson Committed by Dominic Hamon

C++11 concurrency instead of pthread

parent 6b1a6958
......@@ -139,8 +139,8 @@ BENCHMARK(BM_MultiThreaded)->Threads(4);
#include <functional>
#include <memory>
#include <pthread.h>
#include <string>
#include <thread>
#include <vector>
#include "macros.h"
......@@ -263,7 +263,7 @@ class State {
// BenchmarkInstance
SharedState* shared_;
pthread_t thread_;
std::thread thread_;
// Custom label set by the user.
std::string label_;
......
#ifndef BENCHMARK_MUTEX_LOCK_H_
#define BENCHMARK_MUTEX_LOCK_H_
#include <pthread.h>
namespace benchmark {
class mutex_lock {
public:
explicit mutex_lock(pthread_mutex_t* mu) : mu_(mu) {
pthread_mutex_lock(mu_);
}
~mutex_lock() { pthread_mutex_unlock(mu_); }
private:
pthread_mutex_t* mu_;
};
} // end namespace benchmark
#endif // BENCHMARK_MUTEX_LOCK_H_
......@@ -16,7 +16,6 @@
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......@@ -28,18 +27,18 @@
#include <iostream>
#include <limits>
#include <mutex>
#include "benchmark/macros.h"
#include "cycleclock.h"
#include "mutex_lock.h"
#include "sleep.h"
namespace benchmark {
namespace {
pthread_once_t cpuinfo_init = PTHREAD_ONCE_INIT;
std::once_flag cpuinfo_init;
double cpuinfo_cycles_per_second = 1.0;
int cpuinfo_num_cpus = 1; // Conservative guess
pthread_mutex_t cputimens_mutex;
std::mutex cputimens_mutex;
#if !defined OS_MACOSX
const int64_t estimate_time_ms = 1000;
......@@ -76,9 +75,6 @@ bool ReadIntFromFile(const char* file, int* value) {
#endif
void InitializeSystemInfo() {
// TODO: destroy this
pthread_mutex_init(&cputimens_mutex, NULL);
#if defined OS_LINUX || defined OS_CYGWIN
char line[1024];
char* err;
......@@ -315,7 +311,7 @@ static bool MyCPUUsageCPUTimeNsLocked(double* cputime) {
double MyCPUUsage() {
{
mutex_lock l(&cputimens_mutex);
std::lock_guard<std::mutex> l(cputimens_mutex);
static bool use_cputime_ns = true;
if (use_cputime_ns) {
double value;
......@@ -344,12 +340,12 @@ double ChildrenCPUUsage() {
#endif // OS_WINDOWS
double CyclesPerSecond(void) {
pthread_once(&cpuinfo_init, &InitializeSystemInfo);
std::call_once(cpuinfo_init, InitializeSystemInfo);
return cpuinfo_cycles_per_second;
}
int NumCPUs(void) {
pthread_once(&cpuinfo_init, &InitializeSystemInfo);
std::call_once(cpuinfo_init, InitializeSystemInfo);
return cpuinfo_num_cpus;
}
} // end namespace benchmark
# Demonstration executable
add_executable(benchmark_test benchmark_test.cc)
target_link_libraries(benchmark_test benchmark ${CMAKE_THREAD_LIBS_INIT})
add_test(benchmark benchmark_test)
# Test harness for regex wrapper
add_executable(re_test ${RE_FILES} "re_test.cc")
......
......@@ -7,6 +7,7 @@
#include <limits>
#include <list>
#include <map>
#include <mutex>
#include <set>
#include <sstream>
#include <vector>
......@@ -34,7 +35,7 @@ std::set<int> ConstructRandomSet(int size) {
return s;
}
pthread_mutex_t test_vector_mu;
std::mutex test_vector_mu;
std::vector<int>* test_vector = nullptr;
} // end namespace
......@@ -113,23 +114,20 @@ BENCHMARK(BM_StringCompare)->Range(1, 1<<20);
static void BM_SetupTeardown(benchmark::State& state) {
if (state.thread_index == 0) {
pthread_mutex_init(&test_vector_mu, nullptr);
// No need to lock test_vector_mu here as this is running single-threaded.
test_vector = new std::vector<int>();
}
int i = 0;
while (state.KeepRunning()) {
pthread_mutex_lock(&test_vector_mu);
std::lock_guard<std::mutex> l(test_vector_mu);
if (i%2 == 0)
test_vector->push_back(i);
else
test_vector->pop_back();
pthread_mutex_unlock(&test_vector_mu);
++i;
}
if (state.thread_index == 0) {
delete test_vector;
pthread_mutex_destroy(&test_vector_mu);
}
}
BENCHMARK(BM_SetupTeardown)->ThreadPerCpu();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment