提升C++线程
Boost C++ thread
我从以下位置构建了 server3 示例:
http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/examples.html
我唯一做的——修改了request_handler.cpp
:
// Decode url to path.
std::string request_path;
if (!url_decode(req.uri, request_path))
{
rep = reply::stock_reply(reply::bad_request);
return;
}
// Request path must be absolute and not contain "..".
if (request_path.empty() || request_path[0] != '/'
|| request_path.find("..") != std::string::npos)
{
rep = reply::stock_reply(reply::bad_request);
return;
}
// Fill out the reply to be sent to the client.
rep.status = reply::ok;
std::string filename = "/tmp/test.mp4";
std::ifstream file (filename.c_str(), std::ios::in|std::ios::binary);
char buf[1024000]; // 1MB Buffer read
while (file.read(buf, sizeof(buf)).gcount() > 0)
rep.content.append(buf, file.gcount());
rep.headers.resize(9);
rep.headers[0].name = "Content-Length";
rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size());
rep.headers[1].name = "Content-Type";
rep.headers[1].value = "video/mp4";
当我打开chrome并点击服务器时,我可以获取视频,没有问题。同时,我打开另一个选项卡并点击服务器时,没有任何反应。看起来它等到第一个选项卡完成。
目标是拥有一个处理多个连接并发送多个文件的服务器..
服务器的响应能力将基于以下内容:
- 您使用阻止磁盘 IO 调用,因此这将在读取数据时挂起线程。 为了获得最佳性能,您希望尽可能多地使用非阻塞。
- 运行的线程数 io_service::run()。
对您来说,最简单的方法很可能是运行更多运行 io_service::run() 的线程。 我的猜测是你只运行一个线程,这就是为什么在第一个选项卡完成之前你在第二个选项卡中没有得到任何响应的原因。
更好的解决方案是考虑使用非阻塞磁盘io。
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- c++中的线程池
- 线程之间的布尔停止信号
- 为什么std::async使用同一个线程运行函数
- 用于矢量处理的多个线程
- C++为线程工作动态地分割例程
- 为什么我不能在 while 循环中创建线程?