From: Peizhao Ou Date: Sat, 9 Dec 2017 18:26:55 +0000 (-0800) Subject: Uses unique_ptr for Threads X-Git-Url: http://plrg.eecs.uci.edu/git/?p=libcds.git;a=commitdiff_plain;h=dfda714438c69e7c8e3642e45f4decb3c15eb3f8 Uses unique_ptr for Threads --- diff --git a/test/stress/misc/barrier_driver.cpp b/test/stress/misc/barrier_driver.cpp index 438d033a..3ffe5aec 100644 --- a/test/stress/misc/barrier_driver.cpp +++ b/test/stress/misc/barrier_driver.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include 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 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(); } } diff --git a/test/stress/misc/deque_driver.cpp b/test/stress/misc/deque_driver.cpp index e49b4a7a..60809472 100644 --- a/test/stress/misc/deque_driver.cpp +++ b/test/stress/misc/deque_driver.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include 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 threads( + new std::thread[s_nDequeStealerThreadCount]); for (ullong i = 0; i < s_nDequeStealerThreadCount; i++) { threads[i] = std::thread(StealerThread, i); } diff --git a/test/stress/misc/mcslock_driver.cpp b/test/stress/misc/mcslock_driver.cpp index 2a2091b9..e37e4324 100644 --- a/test/stress/misc/mcslock_driver.cpp +++ b/test/stress/misc/mcslock_driver.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include 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 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) { diff --git a/test/stress/misc/rwlock_driver.cpp b/test/stress/misc/rwlock_driver.cpp index 2c61d440..548d16a9 100644 --- a/test/stress/misc/rwlock_driver.cpp +++ b/test/stress/misc/rwlock_driver.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include 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 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(); } } diff --git a/test/stress/misc/seqlock_driver.cpp b/test/stress/misc/seqlock_driver.cpp index abf25837..ba47d4a9 100644 --- a/test/stress/misc/seqlock_driver.cpp +++ b/test/stress/misc/seqlock_driver.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include 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 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; } } diff --git a/test/stress/misc/spinlock_driver.cpp b/test/stress/misc/spinlock_driver.cpp index a811db23..159c18af 100644 --- a/test/stress/misc/spinlock_driver.cpp +++ b/test/stress/misc/spinlock_driver.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include 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(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;