#include <iostream>
#include <thread>
#include <atomic>
#include <mutex>
#include <chrono>
#include <functional>
#include <future>
static uint64_t total_1 = 0;
std::mutex mtx;
void pred1(void)
{
for (uint64_t i = 0; i <= 100000000; i++) {
mtx.lock();
total_1 += i;
mtx.unlock();
}
}
void test1()
{
total_1 = 0;
std::cout << "thread[" << std::this_thread::get_id() << "]-1 start...\n";
auto start = std::chrono::steady_clock::now();
std::thread t1(pred1);
std::thread t2(pred1);
t1.join();
t2.join();
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "test1(mutex/multi-thread) -> " << " total_1: " << total_1 << " elapsed time(ms): " << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_seconds).count() << "ms\n";
std::cout << "test1(mutex/multi-thread) -> " << " total_1: " << total_1 << " elapsed time(s): " << std::chrono::duration_cast<std::chrono::seconds>(elapsed_seconds).count() << "s\n";
}
std::atomic_uint64_t total_2{ 0 };
void pred2(void)
{
for (uint64_t i = 0; i <= 100000000; i++) {
total_2 += i;
}
}
void test2()
{
std::cout << "thread[" << std::this_thread::get_id() << "]-2 start...\n";
auto start = std::chrono::steady_clock::now();
std::thread t1(pred2);
std::thread t2(pred2);
t1.join();
t2.join();
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "test2(atomic/multi-thread) -> " << " total_2: " << total_2 << " elapsed time(ms): " << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_seconds).count() << "ms\n";
std::cout << "test2(atomic/multi-thread) -> " << " total_2: " << total_2 << " elapsed time(s): " << std::chrono::duration_cast<std::chrono::seconds>(elapsed_seconds).count() << "s\n";
}
std::atomic<uint64_t> total_3;
void pred3(void)
{
uint64_t expect = 0;
uint64_t desired = 0;
for (uint64_t i = 0; i <= 100000000; i++) {
do
{
expect = total_3.load();
desired = expect + i;
} while (!total_3.compare_exchange_weak(expect, desired, std::memory_order_release, std::memory_order_relaxed));
//} while (!total_3.compare_exchange_strong(expect, desired, std::memory_order_release, std::memory_order_relaxed));
}
}
void test3()
{
std::cout << "thread[" << std::this_thread::get_id() << "]-3 start...\n";
auto start = std::chrono::steady_clock::now();
std::thread t1(pred3);
std::thread t2(pred3);
t1.join();
t2.join();
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "test3(CAS/multi-thread) -> " << " total_3: " << total_3 << " elapsed time(ms): " << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_seconds).count() << "ms\n";
std::cout << "test3(CAS/multi-thread) -> " << " total_3: " << total_3 << " elapsed time(s): " << std::chrono::duration_cast<std::chrono::seconds>(elapsed_seconds).count() << "s\n";
}
void tesxxx()
{
total_1 = 0;
std::cout << "tesxxx[" << std::this_thread::get_id() << "]-x start...\n";
auto start = std::chrono::steady_clock::now();
pred1();
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "tesxxx(non-multi-thread) -> " << " total_1: " << total_1 << " elapsed time(ms): " << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_seconds).count() << "ms\n";
std::cout << "tesxxx(non-multi-thread) -> " << " total_1: " << total_1 << " elapsed time(s): " << std::chrono::duration_cast<std::chrono::seconds>(elapsed_seconds).count() << "s\n";
}
int main()
{
tesxxx();
test1();
test2();
test3();
}
结果
tesxxx[16840]-x start...
tesxxx(non-multi-thread) -> total_1: 5000000050000000 elapsed time(ms): 8231ms
tesxxx(non-multi-thread) -> total_1: 5000000050000000 elapsed time(s): 8s
thread[16840]-1 start...
test1(mutex/multi-thread) -> total_1: 10000000100000000 elapsed time(ms): 17387ms
test1(mutex/multi-thread) -> total_1: 10000000100000000 elapsed time(s): 17s
thread[16840]-2 start...
test2(atomic/multi-thread) -> total_2: 10000000100000000 elapsed time(ms): 31167ms
test2(atomic/multi-thread) -> total_2: 10000000100000000 elapsed time(s): 31s
thread[16840]-3 start...
test3(CAS/multi-thread) -> total_3: 10000000100000000 elapsed time(ms): 42721ms
test3(CAS/multi-thread) -> total_3: 10000000100000000 elapsed time(s): 42s