如果我使用close而不取消,则存在一些问题.
该close函数可以关闭套接字,并通过返回boost::asio::error::operation_aborted错误来停止任何未完成的异步操作.
我为什么要用cancel而不是close?
我担心一些异步操作是否正在执行,cancel无法取消它,是吗?
就像asio::ip::tcp::resolve::cancel,我尝试多次取消resolve_handler后调用async_resolve,但resolve_handler总是返回没有boost::asio::error::operation_aborted错误.
我认为resolve_handler正在执行?
是?
好的,这是我目前的代码片段:
namespace bai = boost::asio::ip;
bai::tcp::socket tcp_connect(std::string hostname, std::string port) {
try {
boost::asio::io_service io_service;
bai::tcp::resolver resolver(io_service);
// we now try to get a list of endpoints to the server
bai::tcp::resolver::query query(hostname, port);
bai::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
bai::tcp::resolver::iterator end;
// looking for a successful endpoint connection
bai::tcp::socket socket(io_service);
boost::system::error_code error = boost::asio::error::host_not_found;
while (error && endpoint_iterator != end) {
socket.close();
socket.connect(*endpoint_iterator++ , error);
}
if (error) throw boost::system::system_error(error);
return socket;
} catch (std::exception &ex) {
std::cout << "Exception: " << …Run Code Online (Sandbox Code Playgroud) 好的,这真的很难.
我希望能够通过在给定的一组有效索引中选择参数类型来获取参数包的子集,然后使用该参数包作为函数的参数列表.IE:
template <size_t... indices_t>
void func(pack_subset<indices_t..., args_t...>); //incorrect syntax,
//not sure how else to write this
//args_t is predetermined, this
//function is a static member of
//a variadic class template
//For a parameter pack <float, double> and index set 0
void func(float); // <- result of template
Run Code Online (Sandbox Code Playgroud)
我可以得到单个索引的类型,但是一组索引更难一些,特别是当该集合具有可变大小时.其语法是:
pack_index<size_t index_t, typename... args>::type
Run Code Online (Sandbox Code Playgroud)
也许我可以将这些内容串起来,但我不知道如何扩展indices_t,以便为列表中的每个值获取pack_index.
c++ parameters templates template-meta-programming variadic-templates
我正在研究GPU合成音频的可行性,其中每个线程呈现一个样本.这对可以使用的算法提出了一些有趣的限制 - 任何引用前一组样本的算法都不能以这种方式实现.
过滤是这些算法之一.带通,低通或高通 - 所有这些都需要查看生成的最后几个样本以计算结果.这是不可能的,因为尚未生成那些样本.
这使得合成带限波形变得困难.一种方法是使用傅里叶级数对部分进行加法合成.但是,这在O(n)时间运行,并且在GPU上特别慢,以至于并行性增益丢失.如果有一个算法在O(1)时间运行,这将消除分支,并且在处理可听范围时可以快达1000倍.
我特意为锯齿寻找像DSF这样的东西.我一直在尝试手工制作傅里叶系列的简化,但这确实很难.主要是因为它涉及谐波数,AKA是Riemann-Zeta函数的唯一奇点.
是否可以实现恒定时间算法?如果没有,可以证明它不是吗?
algorithm parallel-processing gpgpu signal-processing audio-processing
根据boost文档,在 socket::close()调用时,异步发送,接收或连接操作将立即被取消,并将完成boost::asio::error::operation_aborted错误.
socket::cancel导致所有未完成的异步连接,发送和接收操作立即完成,并且取消操作的处理程序将传递boost::asio::error::operation_aborted错误.
它们之间有什么区别吗?
当我想完成一个套接字连接时,我应该调用哪一个,以便调用它们的回调处理程序时boost::asio::error::operation_aborted出错?
我正在编写一个bash脚本来自动完成一些任务.我要做的一件事就是在目录中的文件名中搜索一个模式,然后遍历结果.
当我运行这个脚本时:
data=$(ls $A_PATH_VAR/*.ext | grep -o '201601[0-9]\{2\}\|201602[0-9]\{2\}')
echo $data
Run Code Online (Sandbox Code Playgroud)
我得到了预期的结果 - $A_PATH_VAR/在扩展名中的文件名中找到的所有匹配项的列表.ext.但是,当我将所述模式存储在变量中然后使用它时,如下所示:
startmo=201601
endmo=201602
mo=$((startmo+1))
grepstr="'$startmo[0-9]\{2\}"
while [ $mo -le $endmo ]
do
grepstr="$grepstr\|$mo[0-9]\{2\}"
mo=$((mo+1))
done
grepstr="$grepstr'"
echo $grepstr # correct
data=$(ls $A_PATH_VAR/*.ext | grep -o $grepstr)
echo $data
Run Code Online (Sandbox Code Playgroud)
模式in $grepstr正确回显 - 也就是说,它包含值'201601[0-9]\{2\}\|201602[0-9]\{2\}',但是$data为空.为什么是这样?
我的解决方案
mo=$((startmo+1))
grepstr="($startmo[0-9][0-9]"
while [ $mo -le $endmo ]
do
grepstr="$grepstr|$mo[0-9][0-9]"
mo=$((mo+1))
done
grepstr="$grepstr)"
files=$(ls $A_PATH_VAR/*.ext)
setopt shwordsplit
for file in $files
do
if [[ $file =~ $grepstr …Run Code Online (Sandbox Code Playgroud) 我有一个自定义类'sau_timer'的代码:
sau_timer::sau_timer(int secs, timerparam f, vector<string> params) : strnd(io),
t(io, boost::posix_time::seconds(secs))
{
assert(secs > 0);
this->f = f;
this->params = params;
t.async_wait(strnd.wrap(boost::bind(&sau_timer::exec, this, _1)));
boost::thread thrd(boost::bind(&boost::asio::io_service::run, &io));
io.run();
}
void sau_timer::exec(const boost::system::error_code&) {
(f)(params);
}
Run Code Online (Sandbox Code Playgroud)
我想要它,这样当我制作一个sau_timer对象时,计时器将启动,但允许程序继续执行.例如,这是main():
int main(int argc, char* argv[])
{
vector<string> args(1);
args[0] = "Hello!";
sau_timer timer_test(3, sau_prompt, args);
args[0] = "First!";
sau_prompt(args);
timer_test.thrd.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的目的是生成timer_test,启动一个在调用sau_prompt("Hello!")之前等待三秒钟的计时器,但是将首先调用sau_prompt("First!").目前,显示在一前提示您好,表明定时器允许它继续之前停止整个程序三秒钟.我希望计时器在后台运行.
我究竟做错了什么?代码编译......
谢谢.
使用boost :: asio的以下代码将无法编译:
#ifndef _SERVER_H_
#define _SERVER_H_
#include "Connection.h"
class Server
{
public:
Server(boost::asio::io_service& io_service);
private:
void start_accept();
void handle_accept(Connection::pointer new_connection,const boost::system::error_code& error);
boost::asio::ip::tcp::acceptor acceptor_;
};
#endif
-------------------------------------------------------------------------------------------------
#include "Server.h"
Server::Server(boost::asio::io_service& io_service)
: acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 9985)){
start_accept();
}
void Server::start_accept(){
Connection::pointer new_connection =
Connection::create(acceptor_.io_service());
acceptor_.async_accept(new_connection->socket(),
boost::bind(&Server::handle_accept, this, new_connection,
boost::asio::placeholders::error));
}
void Server::handle_accept(Connection::pointer new_connection,const boost::system::error_code& error){
if (!error)
{
new_connection->start();
start_accept();
}
}
--------------------------------------------------------------------------------------------------
#include <Server.h>
#include <iostream>
int main()
{
try
{
boost::asio::io_service io_service;
Server server1(io_service);
io_service.run();
}
catch (std::exception& …Run Code Online (Sandbox Code Playgroud) 我得到一个C2440('初始化':无法从'std :: _ Vb_reference <_Alloc>'转换为'bool&'),IntelliSense转换为标题中的错误.
我得到了这个错误所说的内容,而不是为什么会这么说.下面的代码产生此错误:
std::vector<const UINT>::iterator oIter;
oIter = std::find(vecuClassID.begin(), vecuClassID.end(), uClassID);
const UINT uDistance = std::distance(vecuClassID.begin(), oIter);
bool& refbStaticSectionInitialized = *(vecbStaticSectionInitialized.begin() + uDistance);
Run Code Online (Sandbox Code Playgroud)
错误似乎发生在最后一行 - 在Visual Studio中,取消引用运算符以红色下划线.这很令人困惑,因为我的代码与CRITICAL_SECTION完全相同,并且不会产生错误:
std::vector<const UINT>::iterator oIter;
oIter = std::find(vecuClassID.begin(), vecuClassID.end(), uClassID);
const UINT uDistance = std::distance(vecuClassID.begin(), oIter);
CRITICAL_SECTION& refhStaticSection = *(vechStaticSection.begin() + uDistance);
Run Code Online (Sandbox Code Playgroud)
它与bool是一个原始的东西有关吗?
关于boost-asio多线程程序,我无法成功.
由于没有任何关于此的好例子或文档,我希望得到你的帮助:)
简单地说,我认为这段代码可以监听,但是当我想要"缓存"缓冲数据时,它不会打印任何内容或者只听一次并停止.
我的代码是:
void Worker::startThread(int clientNumber) {
cout << "listening: "<< clients[clientNumber]->port << endl;
boost::asio::io_service io_service;
tcp::acceptor acc(io_service, tcp::endpoint(tcp::v4(),portNumber[clientNumber]));
socket_ptr sock(new tcp::socket(io_service));
acc.accept(*sock);
try
{
for (;;) {
char data[max_length];
boost::system::error_code error;
cout << "message?" << endl;
size_t length = sock->read_some(boost::asio::buffer(data), error);
cout << "message :)" << endl;
cout << data << endl;
if(error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.
}
}
catch (std::exception& e)
{
std::cerr …Run Code Online (Sandbox Code Playgroud) bool Connection::Receive(){
std::vector<uint8_t> buf(1000);
boost::asio::async_read(socket_,boost::asio::buffer(buf,1000),
boost::bind(&Connection::handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
int rcvlen=buf.size();
ByteBuffer b((std::shared_ptr<uint8_t>)buf.data(),rcvlen);
if(rcvlen <= 0){
buf.clear();
return false;
}
OnReceived(b);
buf.clear();
return true;
}
Run Code Online (Sandbox Code Playgroud)
该方法工作正常,但仅当我在其中创建断点时才有效。等待接收的时间是否有问题?没有断点,什么也收不到。