草庐IT

async_write

全部标签

c++ - boost::asio::async_read_until 问题

我正在尝试修改来自boostasio的echo服务器示例,当我尝试使用boost::asio::async_read_until时遇到了问题。这是代码:#include#include#include#includeusingboost::asio::ip::tcp;classsession{public:session(boost::asio::io_service&io_service):socket_(io_service){}tcp::socket&socket(){returnsocket_;}voidstart(){std::coutsocket(),boost::bind

c++ - 由于超时取消async_read

我正在尝试围绕async_read编写一个包装器同步方法,以允许在套接字上进行非阻塞读取。根据互联网上的几个例子,我开发了一个似乎几乎正确但不起作用的解决方案。该类声明了这些相关的属性和方法:classcommunications_client{protected:boost::shared_ptr_io_service;boost::shared_ptr_socket;boost::array_data;boost::mutex_mutex;bool_timeout_triggered;bool_message_received;boost::system::error_code_e

c++ - std::async "store"如何成为任意异常?

我无法理解std::async怎么可能存储任何异常,而不仅仅是从std::exception派生的东西。我玩弄了下面的代码#include#include#includevoidf(){std::coutfut=std::async(std::launch::async,f);std::cout我异步启动f(),然后在f中抛出一个int。神奇的是,这个int被std::async返回的future捕获并存储。我知道可以在std::async中catch(...)异常,但后者如何在不知道异常类型的情况下存储它?异常不是从某个基类派生的(在这种情况下,可能可以通过一些Base::clone

c++ - "no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, std::string&)’“

我正在尝试使用std::async创建线程,但我不断收到错误“没有匹配函数调用‘async(std::launch,,std::string&)’”在行上ConnectFuture=std::async(std::launch::async,Connect_T,ip);这是产生这种行为的代码:#includeclasslibWrapper{public:voidConnect(std::stringip);voidConnect_T(std::stringip);private:std::futureConnectFuture;};voidlibWrapper::Connect(std

c++ - 为什么即使使用指定的 std::launch::async 标志,std::async 也会同步调用该函数

我传递给std::async的函数打印当前线程ID。尽管使用std::launch::async标志调用,它仍打印相同的theadid。这意味着它同步调用该函数。为什么?voidPrintThreadId(){std::cout输出是:2093620936209362093620936环境:VS2015,W7。提前致谢! 最佳答案 您实际上通过等待每个调用来序列化调用,因此可以重复使用同一个线程而不会破坏std::future由不同于调用者线程当以下代码显示与其他代码相同的CallerThreadId时,请唤醒我们:voidPrin

c++ - boost async_write 问题

我将展示一些代码;voidwh(constboost::system::error_code&ec,std::size_tbytes_transferred){std::cout当我使用该代码时出现内存泄漏,我发现一些代码(如minicom_client教程)甚至从该代码中变得复杂,而且我在minicom_client上出现内存泄漏。如果我使用boost::asio::write(pSerial,boost::asio::buffer("A",1));代替async_write效果很好,你能解释一下那里发生了什么吗,非常感谢...... 最佳答案

c++ - ostream::write 实际写入了多少字节?

假设我向ostream::write发送了一个大缓冲区,但实际上只有它的开始部分成功写入,其余部分没有写入intmain(){std::vectorbuf(64*1000*1000,'a');//64mbytesofdatastd::ofstreamfile("out.txt");file.write(&buf[0],buf.size());//trytowrite64mbytesif(file.bad()){//butsupposeonly10megabytewereavailableondisk//howmanywereactuallywrittentofile???}return

c++ - std::async 不并行化任务

在此代码段中使用C++11std::async:intfoo(){::sleep(2);return123;}intmain(){futurer1(async(foo));intr2=foo();cout它产生正确的结果,但连续运行两个foo(整个应用程序运行4秒)。编译为:g++-std=gnu++11-O2foo.cc-lpthread(Ubuntu12.1064位,gcc4.7.2) 最佳答案 您可能需要添加launchpolicystd::launch::async的:std::async(std::launch::asyn

c++ - OpenGL 3.3/GLSL 和 C++ 错误 : "must write to gl_Position"

我目前正在尝试使用OpenGL3.3和C++以及GLM、GLFW3和GLEW库来渲染一个三角形,但在尝试创建我的着色器程序时出现错误。Vertexinfo(0):errorC5145:mustwritetogl_Position我已经尝试找出发生这种情况的原因并在其他论坛上询问过,但没有人知道原因是什么。这个错误可能起源于三个可能的点-在我的main.cpp中,我在其中创建窗口、上下文、程序、vao等......#include#include#include#include#include#include"util/shaderutil.hpp"#defineWIDTH800#def

c++ - write_some 与 write - boost asio

当write_some可能无法将所有数据传输到对等端时,为什么有人要使用它?来自boostwrite_some文档Thewrite_someoperationmaynottransmitallofthedatatothepeer.Considerusingthewritefunctionifyouneedtoensurethatalldataiswrittenbeforetheblockingoperationcompletes.write_some方法在boost中有write方法的相关性是什么?我浏览了boostwrite_some文档,我猜不出什么。