thread_pool/main.cpp
2023-04-13 13:18:08 +08:00

54 lines
1.2 KiB
C++

#include <iostream>
#include <random>
#include <csignal>
#include "pool/ThreadPool.h"
std::random_device rd; // 真实随机数产生器
std::mt19937 mt(rd()); //生成计算随机数mt
std::uniform_int_distribution<int> dist(-1000, 1000); //生成-1000到1000之间的离散均匀分布数
auto rnd = std::bind(dist, mt);
// 添加两个数字的简单函数并打印结果
int multiply(const int a, const int b)
{
const int res = a * b;
return res;
}
int main() {
ThreadPool* pool = new ThreadPool(10);
time_t time1 = 0;
time_t time2 = 0;
for (int i = 0; i < 100000; ++i) {
time_t begin = time(NULL);
auto res = pool->pushTask(multiply, 100, 100).get();
printf("a * b = %d\n", res);
time_t end = time(NULL);
time1 += end -begin;
}
for (int i = 0; i < 100000; ++i) {
int a = 100;
int b = 100;
time_t begin = time(NULL);
std::thread t = std::thread([&]() {
const int res = a * b;
printf("a * b = %d\n", res);
});
t.join();
time_t end = time(NULL);
time2 += end - begin;
}
std::cout << time1 << std::endl;
std::cout << time2 << std::endl;
return 0;
}