因为工程需要处理大量的图片,这段时间看了一下C++多线程的相关内容,并参照网上写的多线程类,运用到自己的工程中。
一 多线程
https://blog.csdn.net/invincibleFF/article/details/80048868 这篇文章介绍的很详细。关于多线程是什么。在计算时间相对较长,计算量相对较大的并行计算时,使用多线程可以极大的提高运算速度。
二 多线程的使用
我在网上找的一个多线程类ThreadPool.h,代码如下(如果该代码原作者看到,请留言,由于时间很长,是在找不到原作者了。):
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>
class ThreadPool {
public:
ThreadPool(size_t);
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args)
->std::future<typename std::result_of<F(Args...)>::type>;
~ThreadPool();
private:
// need to keep track of threads so we can join them
std::vector< std::thread > workers;
// the task queue
std::queue< std::function<void()> > tasks;
// synchronization
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;
};
// the constructor just launches some amount of workers
inline ThreadPool::ThreadPool(size_t threads)
: stop(false)
{
for (size_t i = 0; i < threads; ++i)
workers.emplace_back(
[this]
{
for (;;)
{
std::function<void()> task;
{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock,
[this]{ return this->stop || !this->tasks.empty(); });
if (this->stop && this->tasks.empty())
return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
}
);
}
// add new work item to the pool
template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>
{
using return_type = typename std::result_of<F(Args...)>::type;
auto task = std::make_shared< std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex);
// don't allow enqueueing after stopping the pool
if (stop)
throw std::runtime_error("enqueue on stopped ThreadPool");
tasks.emplace([task](){ (*task)(); });
}
condition.notify_one();
return res;
}
// the destructor joins all threads
inline ThreadPool::~ThreadPool()
{
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
condition.notify_all();
for (std::thread &worker : workers)
worker.join();
}
#endif
直接添加这个类。
应用:
double terr(int i)
{
double area_i = i* i * 3.14;
area_i = area_i*area_i;
return area_i;
}
void main()
{
std::mutex mutex_tmp;
float startTime0, endTime0;
//定义线程数
int tread_num = 10;
ThreadPool pool(tread_num);
///任务队列 放任务函数
std::vector< std::future<double> > results;
for (int i = 0; i != 1000000; i++)
{
results.emplace_back(pool.enqueue(bind(terr, i)));
}
//计时
startTime0 = omp_get_wtime();
vector<std::future<double>>::iterator result;
for (result = results.begin(); result != results.end(); result++)
{
mutex_tmp.lock();
result->get();
mutex_tmp.unlock();
}
endTime0 = omp_get_wtime();
cout << tread_num << "个线程处理时间为 : " << endTime0 - startTime0 << endl;
system("pause");
return;
}
results.emplace_back(pool.enqueue(bind(terr, i)));将任务函数放到队列中。并使用result->get();得到最后的计算结果。
以上是C++实现多线程计算的一种方式。还有使用openmp的方式:https://blog.csdn.net/zcgyq/article/details/83088324可以参考这篇博文,关于openmp的用法更为简单。
下面介绍QT多线程用法。