Browse Source

CountBarrierTest通过。
CountBarrier类不会阻塞线程池中的线程,只会阻塞主线程,
而Boost的Barrier类会阻塞线程池中线程

tanghai 14 years ago
parent
commit
a263e4dd0d
2 changed files with 12 additions and 12 deletions
  1. 11 11
      Src/Egametang/Thread/CountBarrierTest.cc
  2. 1 1
      Src/Egametang/Thread/ThreadPool.cc

+ 11 - 11
Src/Egametang/Thread/CountBarrierTest.cc

@@ -1,6 +1,7 @@
 #include <boost/bind.hpp>
 #include <boost/bind.hpp>
 #include <boost/ref.hpp>
 #include <boost/ref.hpp>
 #include <boost/detail/atomic_count.hpp>
 #include <boost/detail/atomic_count.hpp>
+#include <boost/date_time.hpp>
 #include <gtest/gtest.h>
 #include <gtest/gtest.h>
 #include <glog/logging.h>
 #include <glog/logging.h>
 #include <gflags/gflags.h>
 #include <gflags/gflags.h>
@@ -21,11 +22,14 @@ public:
 	void Wait(CountBarrier& barrier)
 	void Wait(CountBarrier& barrier)
 	{
 	{
 		barrier.Wait();
 		barrier.Wait();
-		++count_;
-		VLOG(3) << "count barrier test wait end";
 	}
 	}
 	void Signal(CountBarrier& barrier)
 	void Signal(CountBarrier& barrier)
 	{
 	{
+		boost::xtime xt;
+		boost::xtime_get(&xt, boost::TIME_UTC);
+		xt.sec += 2;
+		boost::thread::sleep(xt);
+		++count_;
 		barrier.Signal();
 		barrier.Signal();
 	}
 	}
 };
 };
@@ -39,22 +43,18 @@ TEST_F(CountBarrierTest, Count)
 TEST_F(CountBarrierTest, WaitAndSignal)
 TEST_F(CountBarrierTest, WaitAndSignal)
 {
 {
 	CountBarrier barrier(10);
 	CountBarrier barrier(10);
-	ThreadPool pool(11);
-	for (int i = 0; i < 10; ++i)
-	{
-		pool.PushTask(
-				boost::bind(&CountBarrierTest::Wait,
-						this, boost::ref(barrier)));
-	}
-	ASSERT_EQ(0, this->count_);
+	ThreadPool pool(10);
+	pool.Start();
 	for (int i = 0; i < 10; ++i)
 	for (int i = 0; i < 10; ++i)
 	{
 	{
 		pool.PushTask(
 		pool.PushTask(
 				boost::bind(&CountBarrierTest::Signal,
 				boost::bind(&CountBarrierTest::Signal,
 						this, boost::ref(barrier)));
 						this, boost::ref(barrier)));
 	}
 	}
-	pool.Stop();
+	ASSERT_EQ(0, this->count_);
+	barrier.Wait();
 	ASSERT_EQ(10, this->count_);
 	ASSERT_EQ(10, this->count_);
+	pool.Stop();
 }
 }
 
 
 } // namespace Egametang
 } // namespace Egametang

+ 1 - 1
Src/Egametang/Thread/ThreadPool.cc

@@ -66,7 +66,7 @@ void ThreadPool::Runner()
 			}
 			}
 			continued = running_ || !tasks_.empty();
 			continued = running_ || !tasks_.empty();
 			VLOG(3) << "continued = " << continued
 			VLOG(3) << "continued = " << continued
-					<< "running = " << running_
+					<< " running = " << running_
 					<< " tasks size = " << tasks_.size();
 					<< " tasks size = " << tasks_.size();
 			VLOG(3) << "loop unlock";
 			VLOG(3) << "loop unlock";
 		}
 		}