Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将Futures与Boost线程池一起使用_C++_Boost_Future - Fatal编程技术网

C++ 将Futures与Boost线程池一起使用

C++ 将Futures与Boost线程池一起使用,c++,boost,future,C++,Boost,Future,我正在实现一个TCP客户机,它可以读取和发送文件和字符串,我正在使用Boost作为我的主库。我希望在继续发送字符串的同时继续读取或发送文件,在这种情况下,字符串是发送到服务器的命令。为此,我考虑使用线程池,以避免客户端过载。我的问题是,当池中线程的on结束时,我可以使用futures来使用回调吗?如果我不能,还有别的解决办法吗? 我在做这样的事情,pool\uucode>是一个boost:asio:thread\upool void send_file(std::string const&

我正在实现一个TCP客户机,它可以读取和发送文件和字符串,我正在使用Boost作为我的主库。我希望在继续发送字符串的同时继续读取或发送文件,在这种情况下,字符串是发送到服务器的命令。为此,我考虑使用线程池,以避免客户端过载。我的问题是,当池中线程的on结束时,我可以使用futures来使用回调吗?如果我不能,还有别的解决办法吗? 我在做这样的事情,
pool\uucode>是一个
boost:asio:thread\upool

void send_file(std::string const& file_path){
    boost::asio::post(pool_, [this, &file_path] {
        handle_send_file(file_path);
    });
    // DO SOMETHING WHEN handle_send_file ENDS
}

void handle_send_file(std::string const& file_path) {
    boost::array<char, 1024> buf{};
    boost::system::error_code error;
    std::ifstream source_file(file_path, std::ios_base::binary | std::ios_base::ate);

    if(!source_file) {
        std::cout << "[ERROR] Failed to open " << file_path << std::endl;
        //TODO gestire errore
    }
    size_t file_size = source_file.tellg();
    source_file.seekg(0);

    std::string file_size_readable = file_size_to_readable(file_size);

    // First send file name and file size in bytes to server
    boost::asio::streambuf request;
    std::ostream request_stream(&request);
    request_stream << file_path << "\n"
                   << file_size << "\n\n"; // Consider sending readable version, does it change anything?

    // Send the request
    boost::asio::write(*socket_, request, error);
    if(error){
        std::cout << "[ERROR] Send request error:" << error << std::endl;
        //TODO lanciare un'eccezione? Qua dovrò controllare se il server funziona o no
    }
    if(DEBUG) {
        std::cout << "[DEBUG] " << file_path << " size is: " << file_size_readable << std::endl;
        std::cout << "[DEBUG] Start sending file content" << std::endl;
    }

    long bytes_sent = 0;
    float percent = 0;
    print_percentage(percent);

    while(!source_file.eof()) {
        source_file.read(buf.c_array(), (std::streamsize)buf.size());

        int bytes_read_from_file = source_file.gcount(); //int is fine because i read at most buf's size, 1024 in this case

        if(bytes_read_from_file<=0) {
            std::cout << "[ERROR] Read file error" << std::endl;
            break;
            //TODO gestire questo errore
        }

        percent = std::ceil((100.0 * bytes_sent) / file_size);
        print_percentage(percent);

        boost::asio::write(*socket_, boost::asio::buffer(buf.c_array(), source_file.gcount()),
                           boost::asio::transfer_all(), error);
        if(error) {
            std::cout << "[ERROR] Send file error:" << error << std::endl;
            //TODO lanciare un'eccezione?
        }

        bytes_sent += bytes_read_from_file;
    }

    std::cout << "\n" << "[INFO] File " << file_path << " sent successfully!" << std::endl;
}
void发送文件(std::string const和文件路径){
boost::asio::post(池路径,&文件路径){
处理发送文件(文件路径);
});
//在handle\u send\u文件结束时执行某些操作
}
无效句柄发送文件(标准::字符串常量和文件路径){
boost::数组buf{};
boost::system::error\u code error;
std::ifstream源文件(文件路径,std::ios_base::binary | std::ios_base::ate);
如果(!源文件){

std::cout在没有线程结束的情况下发布到池端的操作。这就是池线程的全部目的

void send_file(std::string const& file_path){
    post(pool_, [this, &file_path] {
        handle_send_file(file_path);
    });
    // DO SOMETHING WHEN handle_send_file ENDS
}
这有几个问题。最大的问题是,您不应该通过引用捕获
文件路径
,因为参数很快超出范围,并且
handle\u send\u文件
调用将在另一个线程中的未指定时间运行。这是竞争条件和悬空引用。结果

然后

    // DO SOMETHING WHEN handle_send_file ENDS
位于与
handle\u send\u file
没有顺序关系的行上。实际上,它可能会在该操作开始之前运行

简化 以下是一个简化版本:

#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <fstream>
#include <iostream>
namespace asio = boost::asio;
using asio::ip::tcp;
    
static asio::thread_pool pool_;

struct X {
    std::unique_ptr<tcp::socket> socket_;
    
    explicit X(unsigned short port) : socket_(new tcp::socket{ pool_ }) {
        socket_->connect({ {}, port });
    }

    asio::thread_pool pool_;
    std::unique_ptr<tcp::socket> socket_{ new tcp::socket{ pool_ } };

    void send_file(std::string file_path) {
        post(pool_, [=, this] {
            send_file_implementation(file_path);
            // DO SOMETHING WHEN send_file_implementation ENDS
        });
    }

    // throws system_error exception
    void send_file_implementation(std::string file_path) {
        std::ifstream source_file(file_path,
                                  std::ios_base::binary | std::ios_base::ate);
        size_t file_size = source_file.tellg();
        source_file.seekg(0);

        write(*socket_,
                asio::buffer(file_path + "\n" + std::to_string(file_size) + "\n\n"));

        boost::array<char, 1024> buf{};
        while (source_file.read(buf.c_array(), buf.size()) ||
               source_file.gcount() > 0)
        {
            int n = source_file.gcount();

            if (n <= 0) {
                using namespace boost::system;
                throw system_error(errc::io_error, system_category());
            }

            write(*socket_, asio::buffer(buf), asio::transfer_exactly(n));
        }
    }
};
如果您不知道在那里做什么,并且希望在那时为未来做好准备,您可以:

std::future<void> send_file(std::string file_path) {
    std::packaged_task<void()> task([=, this] {
        send_file_implementation(file_path);
    });

    return post(pool_, std::move(task));
}
我在6868/6969上运行两个Netcat来监听时测试了这一点:

nc -l -p 6868 | head& nc -l -p 6969 | md5sum&
./a.out
wait
服务器打印:

Everything completed without error
Everything completed without error
NetCAT打印过滤后的输出:

main.cpp
1907

#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <fstream>
#include <iostream>
#include <future>
namespace asio = boost::asio;
using asio::ip::tcp;
7ecb71992bcbc22bda44d78ad3e2a5ef  -
main.cpp
1907
#包括
#包括
#包括
#包括
#包括
名称空间asio=boost::asio;
使用asio::ip::tcp;
7ECB71992BCC22BDA44D78AD3E2A5EF-


不是魔术:请看

非常感谢!我知道我第一次放置的
//在发送文件时执行某些操作
与发布到池中的函数的结尾无关,但我这样做是为了向您展示我的意思,愚蠢的是,我认为这还不足以将其放置在帖子中。我只需要某种通知n当文件传输结束时,可能不需要使用future,我将坚持使用您给我的第一个解决方案。非常感谢,我也学会了如何使用futures和pool。
Everything completed without error
Everything completed without error
main.cpp
1907

#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <fstream>
#include <iostream>
#include <future>
namespace asio = boost::asio;
using asio::ip::tcp;
7ecb71992bcbc22bda44d78ad3e2a5ef  -