tanghai пре 15 година
родитељ
комит
80ce5dcbc9
3 измењених фајлова са 135 додато и 134 уклоњено
  1. 73 72
      src/thread/thread_pool.cc
  2. 26 26
      src/thread/thread_pool.h
  3. 36 36
      src/thread/thread_pool_test.cc

+ 73 - 72
src/thread/thread_pool.cc

@@ -1,100 +1,101 @@
 #include <glog/logging.h>
 #include <glog/logging.h>
 #include "thread/thread_pool.h"
 #include "thread/thread_pool.h"
 
 
-namespace hainan
+namespace hainan {
+
+ThreadPool::ThreadPool() :
+	num(0), running(false), work_num(0)
 {
 {
-	ThreadPool::ThreadPool():
-			num(0), running(false), work_num(0)
-	{
-	}
-	ThreadPool::~ThreadPool()
-	{
-	}
+}
+ThreadPool::~ThreadPool()
+{
+}
 
 
-	void ThreadPool::Start()
+void ThreadPool::Start()
+{
+	running = true;
+	for(int i = 0; i < num; ++i)
 	{
 	{
-		running = true;
-		for(int i = 0; i < num; ++i)
-		{
-			shared_ptr<thread> t(new thread(bind(&ThreadPool::Runner, this)));
-			threads.push_back(t);
-			t->detach();
-		}
-		work_num = num;
+		shared_ptr<thread> t(new thread(bind(&ThreadPool::Runner, this)));
+		threads.push_back(t);
+		t->detach();
 	}
 	}
+	work_num = num;
+}
 
 
-	void ThreadPool::Stop()
+void ThreadPool::Stop()
+{
+	VLOG(3)<< "Stop";
+	mutex::scoped_lock lock(mtx);
+	running = false;
+	cond.notify_all();
+	while(work_num > 0)
 	{
 	{
-		VLOG(3) << "Stop";
-		mutex::scoped_lock lock(mtx);
-		running = false;
-		cond.notify_all();
-		while(work_num > 0)
-		{
-			VLOG(3) << "done tasks size = " << tasks.size();
-			done.wait(lock);
-		}
+		VLOG(3) << "done tasks size = " << tasks.size();
+		done.wait(lock);
 	}
 	}
+}
 
 
-	void ThreadPool::Runner()
+void ThreadPool::Runner()
+{
+	VLOG(3) << "thread start";
+	bool continued = true;
+	while(continued)
 	{
 	{
-		VLOG(3) << "thread start";
-		bool continued = true;
-		while(continued)
+		function<void (void)> task;
 		{
 		{
-			function<void (void)> task;
+			VLOG(3) << "loop lock";
+			mutex::scoped_lock lock(mtx);
+			VLOG(3) << "loop lock ok";
+			while(running && tasks.empty())
 			{
 			{
-				VLOG(3) << "loop lock";
-				mutex::scoped_lock lock(mtx);
-				VLOG(3) << "loop lock ok";
-				while(running && tasks.empty())
-				{
-					cond.wait(lock);
-					VLOG(3) << "cond";
-				}
-				if(!tasks.empty())
-				{
-					VLOG(3) << "fetch task";
-					task = tasks.front();
-					tasks.pop_front();
-				}
-				continued = running || !tasks.empty();
-				VLOG(3) << "continued = " << continued
-						<< "running = " << running
-						<< " tasks size = " << tasks.size();
-				VLOG(3) << "loop unlock";
+				cond.wait(lock);
+				VLOG(3) << "cond";
 			}
 			}
-
-			if(task)
+			if(!tasks.empty())
 			{
 			{
-				task();
+				VLOG(3) << "fetch task";
+				task = tasks.front();
+				tasks.pop_front();
 			}
 			}
+			continued = running || !tasks.empty();
+			VLOG(3) << "continued = " << continued
+			<< "running = " << running
+			<< " tasks size = " << tasks.size();
+			VLOG(3) << "loop unlock";
 		}
 		}
-		if(__sync_sub_and_fetch(&work_num, 1) == 0)
+
+		if(task)
 		{
 		{
-			VLOG(3) << "work_num = " << work_num;
-			done.notify_one();
+			task();
 		}
 		}
 	}
 	}
+	if(__sync_sub_and_fetch(&work_num, 1) == 0)
+	{
+		VLOG(3) << "work_num = " << work_num;
+		done.notify_one();
+	}
+}
 
 
-	bool ThreadPool::PushTask(function<void (void)> task)
+bool ThreadPool::PushTask(function<void (void)> task)
+{
+	VLOG(3) << "push task";
 	{
 	{
-		VLOG(3) << "push task";
+		mutex::scoped_lock lock(mtx);
+		if(!running)
 		{
 		{
-			mutex::scoped_lock lock(mtx);
-			if(!running)
-			{
-				return false;
-			}
-			tasks.push_back(task);
+			return false;
 		}
 		}
-		VLOG(3) << "push task unlock";
-		cond.notify_one();
-		return true;
+		tasks.push_back(task);
 	}
 	}
+	VLOG(3) << "push task unlock";
+	cond.notify_one();
+	return true;
+}
 
 
-	void ThreadPool::SetNum(int n)
-	{
-		num = n;
-	}
+void ThreadPool::SetNum(int n)
+{
+	num = n;
 }
 }
+
+} // namespace hainan

+ 26 - 26
src/thread/thread_pool.h

@@ -8,33 +8,33 @@
 #include <boost/bind.hpp>
 #include <boost/bind.hpp>
 #include <boost/smart_ptr.hpp>
 #include <boost/smart_ptr.hpp>
 
 
-namespace hainan
+namespace hainan {
+
+using namespace boost;
+using namespace std;
+class ThreadPool
 {
 {
-	using namespace boost;
-	using namespace std;
-	class ThreadPool
-	{
-	private:
-		int num;
-		volatile int work_num;
-		volatile bool running;
-		mutex mtx;
-		condition_variable cond;
-		condition_variable done;
-		list< shared_ptr<thread> > threads;
-		list< function<void (void)> > tasks;
+private:
+	int num;
+	volatile int work_num;
+	volatile bool running;
+	mutex mtx;
+	condition_variable cond;
+	condition_variable done;
+	list<shared_ptr<thread> > threads;
+	list<function<void(void)> > tasks;
 
 
-		void Runner();
+	void Runner();
 
 
-		ThreadPool(ThreadPool const&);
-		ThreadPool operator=(ThreadPool const&);
-	public:
-		ThreadPool();
-		~ThreadPool();
-		void Start();
-		void Stop();
-		void SetNum(int n);
-		bool PushTask(function<void (void)> task);
-	};
-}
+	ThreadPool(ThreadPool const&);
+	ThreadPool operator=(ThreadPool const&);
+public:
+	ThreadPool();
+	~ThreadPool();
+	void Start();
+	void Stop();
+	void SetNum(int n);
+	bool PushTask(function<void(void)> task);
+};
+} // namespace hainan
 #endif  // THREAD_THREAD_POOL_H
 #endif  // THREAD_THREAD_POOL_H

+ 36 - 36
src/thread/thread_pool_test.cc

@@ -5,48 +5,48 @@
 #include <glog/logging.h>
 #include <glog/logging.h>
 #include "thread/thread_pool.h"
 #include "thread/thread_pool.h"
 
 
-namespace hainan
+namespace hainan {
+
+class ThreadPoolTest: public testing::Test
 {
 {
-	class ThreadPoolTest: public testing::Test
+	void SetUp()
+	{
+		thread_pool.SetNum(10);
+		thread_pool.Start();
+	}
+	void TearDown()
 	{
 	{
-		void SetUp()
-		{
-			thread_pool.SetNum(10);
-			thread_pool.Start();
-		}
-		void TearDown()
-		{
-		}
-	protected:
-		ThreadPool thread_pool;
-	public:
-		ThreadPoolTest(): thread_pool()
-		{
-		}
-		void Max(int a, int b, int* z)
-		{
-			*z = a > b? a : b;
-		}
-	};
+	}
+protected:
+	ThreadPool thread_pool;
+public:
+	ThreadPoolTest() :
+		thread_pool()
+	{
+	}
+	void Max(int a, int b, int* z)
+	{
+		*z = a > b? a : b;
+	}
+};
 
 
-	TEST_F(ThreadPoolTest, Test1)
+TEST_F(ThreadPoolTest, Test1)
+{
+	vector<int> x(100, 8);
+	vector<int> y(100, 9);
+	vector<int> z(100, 0);
+	for(int i = 0; i < 100; ++i)
+	{
+		thread_pool.PushTask(
+		        bind(&ThreadPoolTest::Max, this, x[i], y[i], &z[i]));
+	}
+	thread_pool.Stop();
+	for(int i = 0; i < 100; ++i)
 	{
 	{
-		vector<int> x(100, 8);
-		vector<int> y(100, 9);
-		vector<int> z(100, 0);
-		for(int i = 0; i < 100; ++i)
-		{
-			thread_pool.PushTask(
-				bind(&ThreadPoolTest::Max,
-					this, x[i], y[i], &z[i]));
-		}
-		thread_pool.Stop();
-		for(int i = 0; i < 100; ++i)
-		{
-			ASSERT_EQ(9, z[i]) << "i = " << i;
-		}
+		ASSERT_EQ(9, z[i])<< "i = " << i;
 	}
 	}
 }
 }
+} // namespace hainan
 
 
 int main(int argc, char* argv[])
 int main(int argc, char* argv[])
 {
 {