基于boost asio的优先级队列示例,我遇到了设计问题。如果我在处理程序中添加一个包装的处理程序,它似乎迷路了:
我已按原样使用了所有内容,并使用以下代码替换了main()函数:
//
// based on prioritised_handlers.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/asio.hpp>
#include <boost/function.hpp>
#include <iostream>
#include <queue>
using boost::asio::ip::tcp;
class handler_priority_queue
{
public:
void add(int priority, boost::function<void()> function)
{
handlers_.push(queued_handler(priority, function));
}
void execute_all()
{
while (!handlers_.empty())
{
queued_handler handler = handlers_.top();
handler.execute();
handlers_.pop();
}
}
// A generic wrapper class for handlers to allow the invocation to be hooked.
template <typename Handler>
class wrapped_handler
{
public:
wrapped_handler(handler_priority_queue& q, int p, Handler h)
: queue_(q), priority_(p), handler_(h)
{
}
void operator()()
{
handler_();
}
template <typename Arg1>
void operator()(Arg1 arg1)
{
handler_(arg1);
}
template <typename Arg1, typename Arg2>
void operator()(Arg1 arg1, Arg2 arg2)
{
handler_(arg1, arg2);
}
//private:
handler_priority_queue& queue_;
int priority_;
Handler handler_;
};
template <typename Handler>
wrapped_handler<Handler> wrap(int priority, Handler handler)
{
return wrapped_handler<Handler>(*this, priority, handler);
}
private:
class queued_handler
{
public:
queued_handler(int p, boost::function<void()> f)
: priority_(p), function_(f)
{
}
void execute()
{
function_();
}
friend bool operator<(const queued_handler& a,
const queued_handler& b)
{
return a.priority_ < b.priority_;
}
private:
int priority_;
boost::function<void()> function_;
};
std::priority_queue<queued_handler> handlers_;
};
// Custom invocation hook for wrapped handlers.
template <typename Function, typename Handler>
void asio_handler_invoke(Function f,
handler_priority_queue::wrapped_handler<Handler>* h)
{
h->queue_.add(h->priority_, f);
}
void low_priority_handler()
{
std::cout << "Low priority handler\n";
}
int main()
{
//
// BASED ON prioritised_handlers.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
//----------------------------------------------------------------------
using boost::asio::ip::tcp;
boost::asio::io_service io_service;
handler_priority_queue pri_queue;
// Post a completion handler to be run immediately.
io_service.post(pri_queue.wrap(0, low_priority_handler));
// Set a deadline timer to expire immediately.
boost::asio::deadline_timer timer1(io_service);
timer1.expires_at(boost::posix_time::neg_infin);
timer1.async_wait(pri_queue.wrap(42, [](const boost::system::error_code& )
{
std::cout << "now" << std::endl;
}));
// Set a deadline timer to expire later.
boost::asio::deadline_timer timer2(io_service, boost::posix_time::milliseconds(100));
boost::asio::deadline_timer timer3(io_service, boost::posix_time::milliseconds(200));
timer2.async_wait(pri_queue.wrap(100, [&pri_queue, &timer3](const boost::system::error_code& )
{
std::cout << "100ms" << std::endl;
timer3.async_wait(pri_queue.wrap(100, [](const boost::system::error_code& )
{
std::cout << "200ms" << std::endl;
}));
}));
while (io_service.run_one())
{
// The custom invocation hook adds the handlers to the priority queue
// rather than executing them from within the poll_one() call.
while (io_service.poll_one())
;
pri_queue.execute_all();
}
}
//g++ -std=c++14 -Wall -Werror -rdynamic -lboost_system -lboost_thread -lboost_log -lpthread prioritised_handlers.cpp
打印:
now
Low priority handler
100ms
缺少来自timer3的200ms打印输出。基于我的printf调试方法,自定义调用挂钩asio_handler_invoke()永远不会被调用为“200ms”操作。不幸的是我看不出原因。
上述方法有什么问题?
从Technik提示更新代码的其余部分
答案 0 :(得分:1)
任何代码都没有任何问题,除了你保持(或者不是保持)应用程序的方式足够长,以便最后一个处理程序完成。
您正在运行io_service,轮询并运行每个作业。所有作业都得到处理,但它们是延迟的异步调用。您依靠io_service调用来保持应用程序活着,但就io_service而言,所有工作都已完成,因此它会停止阻塞,您的main()函数退出并且您永远不会看到最终处理程序的输出
为了向您证明这一点,只需将io_service
包装在::work
对象中,这将阻止io_service
认为它无法完成工作,因此始终会阻止。
boost::asio::io_service::work w(io_service);
在while (io_service.run_one())
行之前添加此权限。
答案 1 :(得分:0)
我认为克里斯托弗的实施存在一个错误:
std::priority_queue
如果您将新元素添加到 point1 的pop()
,这是相同的优先级(在您的情况下为100) - 处理程序可能会添加到堆顶部。在这种情况下, point2 上的io_service
将弹出我们的新处理程序而不执行。在这种情况下,您的point1
循环将停止,因为没有更多的工作。
我认为你可以交换行point2
和select K, listagg(case when chgv1 = 1 then v1 else null end,',') within group (order by v1) as v1lst, --Only consider cases in listagg when rows have changed
listagg(case when chgv2 = 1 then v2 else null end,',') within group (order by v2) as v2lst --Only consider cases in listagg when rows have changed
from (
select k, v1, v2,
row_number() over (partition by k,v1 order by v1 ) as chgv1, --Detect changes in v1. In this case it's 1.
row_number() over (partition by k,v2 order by v1 ) as chgv2 --Detect changes in v1. In this case it's 1.
from t)
group by k;
来修复它。