浏览代码

change VLOG(2) to VLOG(3)

tanghai 15 年之前
父节点
当前提交
921eb3092f
共有 3 个文件被更改,包括 78 次插入13 次删除
  1. 3 1
      src/experimental/SConscript
  2. 63 0
      src/experimental/boost_asio_test.cc
  3. 12 12
      src/thread/thread_pool.cc

+ 3 - 1
src/experimental/SConscript

@@ -6,6 +6,8 @@ experimental_env.Append(LIBS=[
 	'gflags',
 	'gflags',
 	'glog',
 	'glog',
 	'gtest',
 	'gtest',
+	'boost_system'
 ])
 ])
 
 
-experimental_env.Program('boost_function_test.cc')
+experimental_env.Program('boost_function_test.cc')
+experimental_env.Program('boost_asio_test.cc')

+ 63 - 0
src/experimental/boost_asio_test.cc

@@ -0,0 +1,63 @@
+#include <iostream>
+#include <boost/asio.hpp>
+#include <boost/thread.hpp>
+#include <boost/bind.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+
+class printer
+{
+public:
+	printer(boost::asio::io_service& io) :
+		strand_(io), timer1_(io, boost::posix_time::seconds(1)), timer2_(io,
+		        boost::posix_time::seconds(1)), count_(0)
+	{
+		timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
+		timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
+	}
+
+	~printer()
+	{
+		std::cout << "Final count is " << count_ << "\n";
+	}
+
+	void print1()
+	{
+		if(count_ < 10)
+		{
+			std::cout << "Timer 1: " << count_ << "\n";
+			++count_;
+
+			timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
+			timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
+		}
+	}
+
+	void print2()
+	{
+		if(count_ < 10)
+		{
+			std::cout << "Timer 2: " << count_ << "\n";
+			++count_;
+
+			timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
+			timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
+		}
+	}
+
+private:
+	boost::asio::strand strand_;
+	boost::asio::deadline_timer timer1_;
+	boost::asio::deadline_timer timer2_;
+	int count_;
+};
+
+int main()
+{
+	boost::asio::io_service io;
+	printer p(io);
+	boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
+	io.run();
+	t.join();
+
+	return 0;
+}

+ 12 - 12
src/thread/thread_pool.cc

@@ -26,44 +26,44 @@ namespace hainan
 
 
 	void ThreadPool::Stop()
 	void ThreadPool::Stop()
 	{
 	{
-		VLOG(2) << "Stop";
+		VLOG(3) << "Stop";
 		mutex::scoped_lock lock(mtx);
 		mutex::scoped_lock lock(mtx);
 		running = false;
 		running = false;
 		cond.notify_all();
 		cond.notify_all();
 		while(work_num > 0)
 		while(work_num > 0)
 		{
 		{
-			VLOG(2) << "done tasks size = " << tasks.size();
+			VLOG(3) << "done tasks size = " << tasks.size();
 			done.wait(lock);
 			done.wait(lock);
 		}
 		}
 	}
 	}
 
 
 	void ThreadPool::Runner()
 	void ThreadPool::Runner()
 	{
 	{
-		VLOG(2) << "thread start";
+		VLOG(3) << "thread start";
 		bool continued = true;
 		bool continued = true;
 		while(continued)
 		while(continued)
 		{
 		{
 			function<void (void)> task;
 			function<void (void)> task;
 			{
 			{
-				VLOG(2) << "loop lock";
+				VLOG(3) << "loop lock";
 				mutex::scoped_lock lock(mtx);
 				mutex::scoped_lock lock(mtx);
-				VLOG(2) << "loop lock ok";
+				VLOG(3) << "loop lock ok";
 				while(running && tasks.empty())
 				while(running && tasks.empty())
 				{
 				{
 					cond.wait(lock);
 					cond.wait(lock);
-					VLOG(2) << "cond";
+					VLOG(3) << "cond";
 				}
 				}
 				if(!tasks.empty())
 				if(!tasks.empty())
 				{
 				{
-					VLOG(2) << "fetch task";
+					VLOG(3) << "fetch task";
 					task = tasks.front();
 					task = tasks.front();
 					tasks.pop_front();
 					tasks.pop_front();
 				}
 				}
 				continued = running || !tasks.empty();
 				continued = running || !tasks.empty();
-				VLOG(2) << "continued = " << continued
+				VLOG(3) << "continued = " << continued
 						<< "running = " << running
 						<< "running = " << running
 						<< " tasks size = " << tasks.size();
 						<< " tasks size = " << tasks.size();
-				VLOG(2) << "loop unlock";
+				VLOG(3) << "loop unlock";
 			}
 			}
 
 
 			if(task)
 			if(task)
@@ -73,14 +73,14 @@ namespace hainan
 		}
 		}
 		if(__sync_sub_and_fetch(&work_num, 1) == 0)
 		if(__sync_sub_and_fetch(&work_num, 1) == 0)
 		{
 		{
-			VLOG(2) << "work_num = " << work_num;
+			VLOG(3) << "work_num = " << work_num;
 			done.notify_one();
 			done.notify_one();
 		}
 		}
 	}
 	}
 
 
 	bool ThreadPool::PushTask(function<void (void)> task)
 	bool ThreadPool::PushTask(function<void (void)> task)
 	{
 	{
-		VLOG(2) << "push task";
+		VLOG(3) << "push task";
 		{
 		{
 			mutex::scoped_lock lock(mtx);
 			mutex::scoped_lock lock(mtx);
 			if(!running)
 			if(!running)
@@ -89,7 +89,7 @@ namespace hainan
 			}
 			}
 			tasks.push_back(task);
 			tasks.push_back(task);
 		}
 		}
-		VLOG(2) << "push task unlock";
+		VLOG(3) << "push task unlock";
 		cond.notify_one();
 		cond.notify_one();
 		return true;
 		return true;
 	}
 	}