我正在实现一个TCP客户端,该客户端可以读取和发送文件和字符串,并且我将Boost用作主库。我想继续读取或发送文件,同时继续发送字符串,在这种情况下,这是要发送到服务器的命令。为此,我考虑过使用线程池以不使客户端过载。我的问题是,当池中的线程结束时,我可以使用 future 来使用回调吗?万一我不能,还有其他解决办法吗?
我正在做这样的事情,其中​​pool_boost:asio:thread_pool

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 send_file(std::string const& file_path){
    post(pool_, [this, &file_path] {
        handle_send_file(file_path);
    });
    // DO SOMETHING WHEN handle_send_file ENDS
}
这有几个问题。最大的问题是您不应该通过引用捕获file_path,因为该参数很快就会超出范围,并且handle_send_file调用将在另一个线程中的未指定时间运行。那是比赛条件和悬而未决的引用。 Undefined Behaviour结果。
然后
    // DO SOMETHING WHEN handle_send_file ENDS
在与handle_send_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));
        }
    }
};
现在,您确实可以并行运行这些操作中的几个操作(假设有X的多个实例,因此您有单独的socket_连接)。
最后要做一些事情,只需将代码放在我移动注释的位置:
// DO SOMETHING WHEN send_file_implementation ENDS
如果您不知道该怎么办,并且希望将来做好准备,可以:
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));
}
这个overload of post 神奇地¹返回打包任务的 future 。打包的任务将使用(void)返回值或引发的异常来设置内部 promise 。
实际操作中查看: Live On Coliru
int main() {
    // send two files simultaneously to different connections
    X clientA(6868);
    X clientB(6969);

    std::future<void> futures[] = {
        clientA.send_file("main.cpp"),
        clientB.send_file("main.cpp"),
    };

    for (auto& fut : futures) try {
        fut.get();
        std::cout << "Everything completed without error\n";
    } catch(std::exception const& e) {
        std::cout << "Error occurred: " << e.what() << "\n";
    };

    pool_.join();
}
我在运行两个网猫以收听6868/6969时进行了测试:
nc -l -p 6868 | head& nc -l -p 6969 | md5sum&
./a.out
wait
服务器打印:
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  -

¹不是魔术:请参阅https://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/async_result.html

10-07 23:26