Thread pool tidying.
Make fields const. Fix memory leak in gtest caused by not allowing thread pool
to empty tasks.
Change-Id: I32b20dee17eab03443c4bd1f3cc102c4409d1ab9
diff --git a/src/thread_pool.cc b/src/thread_pool.cc
index a06f9d3..370e4bc 100644
--- a/src/thread_pool.cc
+++ b/src/thread_pool.cc
@@ -76,6 +76,8 @@
started_(false),
shutting_down_(false),
waiting_count_(0),
+ start_time_(0),
+ total_wait_time_(0),
// Add one since the caller of constructor waits on the barrier too.
creation_barier_(num_threads + 1) {
Thread* self = Thread::Current();
diff --git a/src/thread_pool.h b/src/thread_pool.h
index e97d7d6..18af97d 100644
--- a/src/thread_pool.h
+++ b/src/thread_pool.h
@@ -50,7 +50,7 @@
static void* Callback(void* arg) LOCKS_EXCLUDED(Locks::mutator_lock_);
virtual void Run();
- ThreadPool* thread_pool_;
+ ThreadPool* const thread_pool_;
const std::string name_;
const size_t stack_size_;
pthread_t pthread_;
diff --git a/src/thread_pool_test.cc b/src/thread_pool_test.cc
index bac6002..e056935 100644
--- a/src/thread_pool_test.cc
+++ b/src/thread_pool_test.cc
@@ -25,11 +25,13 @@
class CountTask : public Task {
public:
- CountTask(AtomicInteger* count) : count_(count) {
-
+ CountTask(AtomicInteger* count) : count_(count), verbose_(false) {
}
- void Run(Thread* /* self */) {
+ void Run(Thread* self) {
+ if (verbose_) {
+ LOG(INFO) << "Running: " << *self;
+ }
// Simulate doing some work.
usleep(100);
// Increment the counter which keeps track of work completed.
@@ -37,11 +39,15 @@
}
void Finalize() {
+ if (verbose_) {
+ LOG(INFO) << "Finalizing: " << *Thread::Current();
+ }
delete this;
}
private:
AtomicInteger* const count_;
+ const bool verbose_;
};
class ThreadPoolTest : public CommonTest {
@@ -87,6 +93,12 @@
usleep(200);
// Ensure that the task added after the workers were stopped doesn't get run.
EXPECT_EQ(0, bad_count);
+ // Allow tasks to finish up and delete themselves.
+ thread_pool.StartWorkers(self);
+ while (count.get() != num_tasks && bad_count.get() != 1) {
+ usleep(200);
+ }
+ thread_pool.StopWorkers(self);
}
class TreeTask : public Task {