You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;
// 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;
// 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_lockstd::mutex lock(queue_mutex);
stop = true;
}
condition.notify_all();
for (std::thread& worker : workers)
worker.join();
}
#ifndef CNN_ARCHITECTURES_H
#define CNN_ARCHITECTURES_H
// C++
#include
#include
// self
#include "pipeline.h"
namespace architectures {
using namespace pipeline;
我试图使用 多线程包装函数,并使用了 2.2万张图片测试多线程,耗时 420s,多线程不可以完全返回结果。
我查找到 原因是 因为 no_grad 这个变量是一个 可读写的全局变量, 问题应该是 这个全局变量 在多线程中被读写导致的错误。
下面是我添加的 多线程
// #include "pool_number.cpp" 我把线程文件放到这里
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include
#include
#include
#include
#include
#include <condition_variable>
#include
#include
#include
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;
};
// 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;
}
// 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;
}
// the destructor joins all threads
inline ThreadPool::~ThreadPool()
{
{
std::unique_lockstd::mutex lock(queue_mutex);
stop = true;
}
condition.notify_all();
for (std::thread& worker : workers)
worker.join();
}
#endif
#include
#include <windows.h>
#include
#include <basci/basci.h>
// #include
// #include "pool_number.cpp"
#include
#include
#include
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include "func.h"
#include "architectures.h"
int cpu_number() {
unsigned int numCores = std::thread::hardware_concurrency();
return numCores;
}
namespace {
void cv_show(const cv::Mat& one_image, const char* info="") {
cv::imshow(info, one_image);
cv::waitKey(0);
cv::destroyAllWindows();
}
}
#include
#include
std::vectorstd::string get_png_list(int main_i,vector images_list){
// 输出不要放在缓冲区, 到时间了及时输出
std::setbuf(stdout, 0);
// std::cout<<"\n\n\n main_list = [\n";
// 逐一读取图像, 做变换
Json::Value root;
vector json_list;
json_list.push_back(to_string(main_i));
for(const auto& image_path : images_list) {
// try{
// 读取图像
cv::Mat origin = cv::imread(image_path);
if(origin.empty() || !std::filesystem::exists(image_path)) {
std::cout << "Failed to read image file " << image_path << "\n";
continue;
}
// // 图像 resize 到规定的大小, 224 X 224
// cv::resize(origin, origin, {std::get<1>(image_size), std::get<2>(image_size)});
// // 转化为 tensor 数据
// image_buffer[0]->read_from_opencv_mat(origin.data);
// // 经过卷积神经网络得到输出
// const auto output = network.forward(image_buffer);
// // softmax 得到输出
// const auto prob = softmax(output);
// // 找到最大概率的输出
// const int max_index = prob[0]->argmax();
}
int main(){
u_init();
std::string read_path = main_path+ utf8togbk("/png");
std::cout<<"exe_path "<<read_path<<std::endl;
ulist main_list =get_all_file(read_path);
//main_list.print();
cout<<"list len "<<main_list.len()<<endl;
}
The text was updated successfully, but these errors were encountered: