From 365c864a587365fe443b11cc0cd7cfc8f8f8eb81 Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期二, 01 六月 2021 11:19:22 +0800 Subject: [PATCH] refactor, clean up useless code. --- utest/robust_test.cpp | 129 ------------------------------------------ 1 files changed, 1 insertions(+), 128 deletions(-) diff --git a/utest/robust_test.cpp b/utest/robust_test.cpp index ea6144c..3270481 100644 --- a/utest/robust_test.cpp +++ b/utest/robust_test.cpp @@ -20,7 +20,7 @@ { AtomicReqRep rr; auto client = [&]() { - for (int i = 0; i < 20; ++i) { + for (int i = 0; i < 5; ++i) { int64_t reply = 0; bool r = rr.ClientRequest(i, reply); printf("init request %d, %s, reply %d\n", i, (r ? "ok" : "failed"), reply); @@ -196,130 +196,3 @@ printf("total: %ld, expected: %ld\n", total.load(), correct_total); BOOST_CHECK_EQUAL(total.load(), correct_total); } - -BOOST_AUTO_TEST_CASE(MutexTest) -{ - { - int sem_id = semget(100, 1, 0666 | IPC_CREAT); - auto P = [&]() { - sembuf op = {0, -1, SEM_UNDO}; - semop(sem_id, &op, 1); - }; - auto V = [&]() { - sembuf op = {0, 1, SEM_UNDO}; - semop(sem_id, &op, 1); - }; - for (int i = 0; i < 10; ++i) { - V(); - } - Sleep(10s); - - return; - } - - // typedef robust::MFMutex RobustMutex; - typedef robust::SemMutex RobustMutex; - - for (int i = 0; i < 20; ++i) { - int size = i; - int left = size & 7; - int rsize = size + ((8 - left) & 7); - printf("size: %3d, rsize: %3d\n", size, rsize); - } - SharedMemory &shm = TestShm(); - // shm.Remove(); - // return; - GlobalInit(shm); - - const std::string mtx_name("test_mutex"); - const std::string int_name("test_int"); - // auto mtx = shm.FindOrCreate<RobustMutex>(mtx_name, 12345); - RobustMutex rmtx(12345); - auto mtx = &rmtx; - auto pi = shm.FindOrCreate<int>(int_name, 100); - - std::mutex m; - typedef std::chrono::steady_clock Clock; - - if (pi) { - auto old = *pi; - printf("int : %d, add1: %d\n", old, ++*pi); - } - - auto LockSpeed = [](auto &mutex, const std::string &name) { - const int ntimes = 1000 * 1; - auto Lock = [&]() { - for (int i = 0; i < ntimes; ++i) { - mutex.lock(); - mutex.unlock(); - } - }; - printf("\nTesting %s lock/unlock %d times\n", name.c_str(), ntimes); - { - boost::timer::auto_cpu_timer timer; - printf("1 thread: "); - Lock(); - } - auto InThread = [&](int nthread) { - boost::timer::auto_cpu_timer timer; - printf("%d threads: ", nthread); - std::vector<std::thread> vt; - for (int i = 0; i < nthread; ++i) { - vt.emplace_back(Lock); - } - for (auto &t : vt) { - t.join(); - } - }; - InThread(4); - InThread(16); - InThread(100); - InThread(1000); - }; - NullMutex null_mtx; - std::mutex std_mtx; - CasMutex cas_mtx; - FMutex mfmtx(3); - boost::interprocess::interprocess_mutex ipc_mutex; - SemMutex sem_mtx(3); - LockSpeed(null_mtx, "null mutex"); - LockSpeed(std_mtx, "std::mutex"); - // LockSpeed(cas_mtx, "CAS mutex"); - LockSpeed(ipc_mutex, "boost ipc mutex"); - LockSpeed(mfmtx, "mutex+flock"); - LockSpeed(sem_mtx, "sem mutex"); - - auto TryLock = [&]() { - if (mtx->try_lock()) { - printf("try_lock ok\n"); - return true; - } else { - printf("try_lock failed\n"); - return false; - } - }; - auto Unlock = [&]() { - mtx->unlock(); - printf("unlocked\n"); - }; - - if (mtx) { - printf("mtx exists\n"); - if (TryLock()) { - // Sleep(10s); - auto op = [&]() { - if (TryLock()) { - Unlock(); - } - }; - op(); - std::thread t(op); - t.join(); - // Unlock(); - } else { - // mtx->unlock(); - } - } else { - printf("mtx not exists\n"); - } -} \ No newline at end of file -- Gitblit v1.8.0