Просмотр исходного кода

调整了一些格式,各种shared_ptr使用typedef

tanghai 15 лет назад
Родитель
Сommit
a1e70284d2
4 измененных файлов с 12 добавлено и 14 удалено
  1. 2 3
      SConstruct
  2. 1 1
      src/thread/thread_pool.cc
  3. 8 8
      src/thread/thread_pool.h
  4. 1 2
      src/thread/thread_pool_test.cc

+ 2 - 3
SConstruct

@@ -1,15 +1,14 @@
+env = DefaultEnvironment()
 
 
 def ParseOptions():
 def ParseOptions():
 	AddOption('--opt',
 	AddOption('--opt',
 		dest='opt',
 		dest='opt',
 		action='store_true',
 		action='store_true',
 		default='false',
 		default='false',
-		help='build in opt mode, or dbg mode')
+		help='build in opt mode, default dbg mode')
 
 
 ParseOptions()
 ParseOptions()
 
 
-env = DefaultEnvironment()
-
 env.Append(LIBS=[
 env.Append(LIBS=[
 	'gflags',
 	'gflags',
 	'glog',
 	'glog',

+ 1 - 1
src/thread/thread_pool.cc

@@ -16,7 +16,7 @@ void ThreadPool::Start()
 	running = true;
 	running = true;
 	for(int i = 0; i < num; ++i)
 	for(int i = 0; i < num; ++i)
 	{
 	{
-		shared_ptr<thread> t(new thread(bind(&ThreadPool::Runner, this)));
+		thread_ptr t(new thread(bind(&ThreadPool::Runner, this)));
 		threads.push_back(t);
 		threads.push_back(t);
 		t->detach();
 		t->detach();
 	}
 	}

+ 8 - 8
src/thread/thread_pool.h

@@ -10,9 +10,12 @@
 
 
 namespace hainan {
 namespace hainan {
 
 
-using namespace boost;
 using namespace std;
 using namespace std;
-class ThreadPool
+using namespace boost;
+
+typedef shared_ptr<thread> thread_ptr;
+
+class ThreadPool: private noncopyable
 {
 {
 private:
 private:
 	int num;
 	int num;
@@ -21,20 +24,17 @@ private:
 	mutex mtx;
 	mutex mtx;
 	condition_variable cond;
 	condition_variable cond;
 	condition_variable done;
 	condition_variable done;
-	list<shared_ptr<thread> > threads;
-	list<function<void(void)> > tasks;
+	list<thread_ptr> threads;
+	list< function<void (void)> > tasks;
 
 
 	void Runner();
 	void Runner();
-
-	ThreadPool(ThreadPool const&);
-	ThreadPool operator=(ThreadPool const&);
 public:
 public:
 	ThreadPool();
 	ThreadPool();
 	~ThreadPool();
 	~ThreadPool();
 	void Start();
 	void Start();
 	void Stop();
 	void Stop();
 	void SetNum(int n);
 	void SetNum(int n);
-	bool PushTask(function<void(void)> task);
+	bool PushTask(function<void (void)> task);
 };
 };
 } // namespace hainan
 } // namespace hainan
 #endif  // THREAD_THREAD_POOL_H
 #endif  // THREAD_THREAD_POOL_H

+ 1 - 2
src/thread/thread_pool_test.cc

@@ -20,8 +20,7 @@ class ThreadPoolTest: public testing::Test
 protected:
 protected:
 	ThreadPool thread_pool;
 	ThreadPool thread_pool;
 public:
 public:
-	ThreadPoolTest() :
-		thread_pool()
+	ThreadPoolTest() : thread_pool()
 	{
 	{
 	}
 	}
 	void Max(int a, int b, int* z)
 	void Max(int a, int b, int* z)