#include <cds/sync/barrier.h>
#include <cds_test/stress_test.h>
#include <iostream>
+#include <memory>
#include <thread>
using namespace std;
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");
};
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();
}
}
#include <cstdlib>
#include <ctime>
#include <iostream>
+#include <memory>
#include <thread>
using namespace std;
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);
}
#include <cds/sync/mcs-lock.h>
#include <cds_test/stress_test.h>
#include <iostream>
+#include <memory>
#include <thread>
using namespace std;
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) {
#include <cds/sync/rwlock.h>
#include <cds_test/stress_test.h>
#include <iostream>
+#include <memory>
#include <thread>
using namespace std;
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() {
}
};
-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();
}
}
#include <cds/sync/seqlock.h>
#include <cds_test/stress_test.h>
#include <iostream>
+#include <memory>
#include <thread>
using namespace std;
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");
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 {
}
};
-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;
}
}
#include <cds_test/stress_test.h>
#include <iostream>
#include <iostream>
+#include <memory>
#include <thread>
using namespace std;
#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(); \
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;
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;