Uses unique_ptr for Threads
authorPeizhao Ou <peizhaoo@uci.edu>
Sat, 9 Dec 2017 18:26:55 +0000 (10:26 -0800)
committerPeizhao Ou <peizhaoo@uci.edu>
Sat, 9 Dec 2017 18:26:55 +0000 (10:26 -0800)
test/stress/misc/barrier_driver.cpp
test/stress/misc/deque_driver.cpp
test/stress/misc/mcslock_driver.cpp
test/stress/misc/rwlock_driver.cpp
test/stress/misc/seqlock_driver.cpp
test/stress/misc/spinlock_driver.cpp

index 438d033a88798b1949ebe1d4f9525ffa4de4093c..3ffe5aec36de10dbf483b1f16d436ecd59664e80 100644 (file)
@@ -5,6 +5,7 @@
 #include <cds/sync/barrier.h>
 #include <cds_test/stress_test.h>
 #include <iostream>
+#include <memory>
 #include <thread>
 
 using namespace std;
@@ -19,7 +20,7 @@ static size_t s_nBarrierPassCount = 100000000;
 class BarrierTest : public cds_test::stress_fixture {
 protected:
   static Barrier *barrier;
-  static int count;
+  static size_t count;
 
   static void SetUpTestCase() {
     cds_test::config const &cfg = get_config("Misc");
@@ -43,16 +44,16 @@ protected:
 };
 
 Barrier *BarrierTest::barrier;
-int BarrierTest::count;
+size_t BarrierTest::count;
 
 TEST_F(BarrierTest, Wait) {
   barrier = new Barrier(s_nBarrierThreadCount);
-  std::thread *threads = new std::thread[s_nBarrierThreadCount];
-  for (int i = 0; i < s_nBarrierThreadCount; i++) {
+  std::unique_ptr<std::thread[]> threads(new std::thread[s_nBarrierThreadCount]);
+  for (size_t i = 0; i < s_nBarrierThreadCount; i++) {
     threads[i] = std::thread(Thread);
   }
 
-  for (int i = 0; i < s_nBarrierThreadCount; i++) {
+  for (size_t i = 0; i < s_nBarrierThreadCount; i++) {
     threads[i].join();
   }
 }
index e49b4a7a0e664e089f00d1de7f4bcb4f603f79d3..60809472bfffdc1b9ba7b6c06c1c76822ec36981 100644 (file)
@@ -4,6 +4,7 @@
 #include <cstdlib>
 #include <ctime>
 #include <iostream>
+#include <memory>
 #include <thread>
 
 using namespace std;
@@ -82,7 +83,8 @@ TEST_F(ChaseLevDequeTest, DequePushPopTake) {
   srand(time(NULL));
 
   // Stealer threads
-  std::thread *threads = new std::thread[s_nDequeStealerThreadCount];
+  std::unique_ptr<std::thread[]> threads(
+      new std::thread[s_nDequeStealerThreadCount]);
   for (ullong i = 0; i < s_nDequeStealerThreadCount; i++) {
     threads[i] = std::thread(StealerThread, i);
   }
index 2a2091b93231f79b7591feb50b38b10262915b92..e37e4324ae86d9f9ed99cd3f5a0ecbf4d95743af 100644 (file)
@@ -5,6 +5,7 @@
 #include <cds/sync/mcs-lock.h>
 #include <cds_test/stress_test.h>
 #include <iostream>
+#include <memory>
 #include <thread>
 
 using namespace std;
@@ -43,11 +44,12 @@ cds_others::mcs_mutex *MCSLockTest::my_mutex;
 TEST_F(MCSLockTest, BasicLockUnlock) {
   my_mutex = new cds_others::mcs_mutex();
   x = 0;
-  std::thread *threads = new std::thread[s_nMCSLockThreadCount];
-  for (int i = 0; i < s_nMCSLockThreadCount; i++) {
+  std::unique_ptr<std::thread[]> threads(
+      new std::thread[s_nMCSLockThreadCount]);
+  for (size_t i = 0; i < s_nMCSLockThreadCount; i++) {
     threads[i] = std::thread(Thread);
   }
-  for (int i = 0; i < s_nMCSLockThreadCount; i++) {
+  for (size_t i = 0; i < s_nMCSLockThreadCount; i++) {
     threads[i].join();
   }
   if (x != s_nMCSLockPassCount * s_nMCSLockThreadCount) {
index 2c61d440e4a75c3e563db772e35d177f98aafb95..548d16a9d95466978d3a8eea92abf0e628abc93b 100644 (file)
@@ -5,6 +5,7 @@
 #include <cds/sync/rwlock.h>
 #include <cds_test/stress_test.h>
 #include <iostream>
+#include <memory>
 #include <thread>
 
 using namespace std;
@@ -17,8 +18,8 @@ static size_t s_nRWLockPassCount = 200000;
 typedef cds_others::RWLock RWLock;
 class RWLockTest : public cds_test::stress_fixture {
 protected:
-  static int sum;
-  static int x;
+  static size_t sum;
+  static size_t x;
   static RWLock *rwlock;
 
   static void SetUpTestCase() {
@@ -58,20 +59,20 @@ protected:
   }
 };
 
-int RWLockTest::x;
-int RWLockTest::sum;
+size_t RWLockTest::x;
+size_t RWLockTest::sum;
 RWLock *RWLockTest::rwlock;
 
 TEST_F(RWLockTest, BasicLockUnlock) {
   rwlock = new RWLock();
-  int num_threads = s_nRWLockThreadCount;
+  size_t num_threads = s_nRWLockThreadCount;
   for (int write_percentage = 5; write_percentage < 40; write_percentage += 5) {
-    std::thread *threads = new std::thread[num_threads];
+    std::unique_ptr<std::thread[]> threads(new std::thread[num_threads]);
     for (size_t i = 0; i < num_threads; i++) {
       threads[i] = std::thread(ReaderWriterThread, write_percentage);
     }
 
-    for (int i = 0; i < num_threads; i++) {
+    for (size_t i = 0; i < num_threads; i++) {
       threads[i].join();
     }
   }
index abf2583701ff72198edad7a494bcf69330b48665..ba47d4a9e76c52d7031ac1e226414ec6d9003d14 100644 (file)
@@ -5,6 +5,7 @@
 #include <cds/sync/seqlock.h>
 #include <cds_test/stress_test.h>
 #include <iostream>
+#include <memory>
 #include <thread>
 
 using namespace std;
@@ -18,11 +19,8 @@ static size_t s_nSeqLockPassCount = 2000000;
 
 class SeqLockTest : public cds_test::stress_fixture {
 protected:
-  static int sum;
+  static size_t sum;
   static SeqLock *seqlock;
-  static const int kReaderThreads = 0;
-  static const int kWriterThreads = 0;
-  static const int kWriterPercentage = 15;
 
   static void SetUpTestCase() {
     cds_test::config const &cfg = get_config("Misc");
@@ -30,12 +28,8 @@ protected:
     GetConfig(SeqLockPassCount);
   }
 
-  static void ReaderThread() {}
-
-  static void WriterThread() {}
-
   static void ReaderWriterThread(int write_percentage) {
-    for (int i = 0; i < s_nSeqLockPassCount; i++) {
+    for (size_t i = 0; i < s_nSeqLockPassCount; i++) {
       if (rand(100) < write_percentage) {
         sum += seqlock->read();
       } else {
@@ -45,20 +39,20 @@ protected:
   }
 };
 
-int SeqLockTest::sum;
+size_t SeqLockTest::sum;
 SeqLock *SeqLockTest::seqlock;
 
 TEST_F(SeqLockTest, BasicReadWriter) {
   seqlock = new SeqLock();
   for (int write_percentage = 5; write_percentage < 50; write_percentage += 5) {
-    std::thread *threads = new std::thread[s_nSeqLockReaderWriterThreadCount];
+    std::unique_ptr<std::thread[]> threads(
+        new std::thread[s_nSeqLockReaderWriterThreadCount]);
     for (size_t i = 0; i < s_nSeqLockReaderWriterThreadCount; i++) {
       threads[i] = std::thread(ReaderWriterThread, write_percentage);
     }
-    for (int i = 0; i < s_nSeqLockReaderWriterThreadCount; i++) {
+    for (size_t i = 0; i < s_nSeqLockReaderWriterThreadCount; i++) {
       threads[i].join();
     }
-    delete[] threads;
   }
 }
 
index a811db23f30279c810cd3f4d8252d0d1521c77dc..159c18af76018aa139d16a188f00ebe147b1f139 100644 (file)
@@ -7,6 +7,7 @@
 #include <cds_test/stress_test.h>
 #include <iostream>
 #include <iostream>
+#include <memory>
 #include <thread>
 
 using namespace std;
@@ -23,7 +24,7 @@ static size_t s_nTicketLockPassCount = 4000000;
 
 #define TASK(lock_type, lock_ptr, pass_cnt)                                    \
   static void Thread##lock_type() {                                            \
-    for (int i = 0; i < pass_cnt; i++) {                                       \
+    for (size_t i = 0; i < pass_cnt; i++) {                                    \
       lock_ptr->lock();                                                        \
       x++;                                                                     \
       lock_ptr->unlock();                                                      \
@@ -34,21 +35,26 @@ static size_t s_nTicketLockPassCount = 4000000;
   TEST_F(SpinLockTest, lock_type) {                                            \
     lock_ptr = new lock_type();                                                \
     x = 0;                                                                     \
+    std::unique_ptr<std::thread[]>(new std::thread[s_nSpinLockThreadCount]);   \
     std::thread *threads = new std::thread[s_nSpinLockThreadCount];            \
-    for (int i = 0; i < s_nSpinLockThreadCount; i++) {                         \
+    for (size_t i = 0; i < s_nSpinLockThreadCount; i++) {                      \
       threads[i] = std::thread(Thread##lock_type);                             \
     }                                                                          \
-    for (int i = 0; i < s_nSpinLockThreadCount; i++) {                         \
+    for (size_t i = 0; i < s_nSpinLockThreadCount; i++) {                      \
       threads[i].join();                                                       \
     }                                                                          \
     if (x != s_nSpinLockThreadCount * pass_cnt) {                              \
       cout << "Incorrect " << #lock_type << "\n";                              \
+      cout << "x=" << x << "\nThreadCount=" << s_nSpinLockThreadCount          \
+           << "\nPassCount=" << pass_cnt                                       \
+           << "\t&&\tSupposed times=" << s_nSpinLockThreadCount * pass_cnt     \
+           << "\n";                                                            \
     }                                                                          \
   }
 
 class SpinLockTest : public cds_test::stress_fixture {
 protected:
-  static int x;
+  static size_t x;
   static TicketLock *ticket_mutex;
   static SpinLock *spin_mutex;
   static Reentrant32 *reentrant_mutex32;
@@ -67,7 +73,7 @@ protected:
   TASK(Reentrant64, reentrant_mutex64, s_nSpinLockPassCount)
 };
 
-int SpinLockTest::x;
+size_t SpinLockTest::x;
 TicketLock *SpinLockTest::ticket_mutex;
 SpinLock *SpinLockTest::spin_mutex;
 Reentrant32 *SpinLockTest::reentrant_mutex32;