gpt4 book ai didi

c++ - async_wait 中的 asio high_resolution_timer 段错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:47:18 32 4
gpt4 key购买 nike

我已经实现了一个优先级从asio examples开始的任务队列和一个使用这个队列的定时器类。这是代码:

priority_task_queue.h

class handler_priority_queue
{
private:
class queued_handler{
private:
size_t _priority;
std::function<void()> _function;
public:
queued_handler(size_t p, std::function<void()> f): _priority(p), _function(f){}
friend bool operator<(const queued_handler& a, const queued_handler& b){
return a._priority < b._priority;
}
void operator()() {
_function();
}
};
std::priority_queue<queued_handler> _handlers;

public:
// A generic wrapper class for handlers to allow the invocation to be hooked.
template <typename Handler> class wrapped_handler
{
private:
handler_priority_queue& _queue;
size_t _priority;
Handler _handler;

public:
handler_priority_queue& queue() {return _queue;}
size_t priority() {return _priority;}

wrapped_handler(handler_priority_queue& q, size_t p, Handler h)
: _queue(q), _priority(p), _handler(h){}


template <typename ...Args>
void operator()(Args&&... args){
_handler(std::forward<Args>(args)...);
}
};
template <typename Handler> wrapped_handler<Handler> wrap(size_t priority, Handler handler){
return wrapped_handler<Handler>(*this, priority, handler);
}
void add(size_t priority, std::function<void()> function);
void execute_all();
void execute_one();
bool empty();
};

// 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);
std::cout<<"LLAMANDO AL INVOKE"<<std::endl; //BORRAR!!
}

class C_priority_task_queue{

private:
asio::io_service& _io;
handler_priority_queue _pri_queue;

public:
template <typename Handler> handler_priority_queue::wrapped_handler<Handler> wrap(int priority, Handler handler){
return _pri_queue.wrap(priority, handler);
}

explicit C_priority_task_queue(asio::io_service& io): _io(io){}
C_priority_task_queue(C_priority_task_queue const&) = delete;
C_priority_task_queue& operator =(C_priority_task_queue const&) = delete;

asio::io_service& io() {return _io;}
void run();
};

priority_task_queue.cpp

void handler_priority_queue::add(size_t priority, std::function<void()> function){
_handlers.push(queued_handler(priority, function));
}

void handler_priority_queue::execute_one(){
if(!_handlers.empty()){
queued_handler handler = _handlers.top();
handler();
_handlers.pop();
}
}

bool handler_priority_queue::empty(){
return _handlers.empty();
}

void C_priority_task_queue::run(){
while (_io.run_one())
{
_io.poll();
while(!_pri_queue.empty())
{
_io.poll();
_pri_queue.execute_one();
}
}
}

base_timer.h

class C_timer {
private:
asio::high_resolution_timer _timer;
uint8_t _timer_id;
C_priority_task_queue& _prio_queue;


void timer_handler_internal(const asio::error_code& e, uint8_t timer_id, const uint64_t sched_time);
virtual void timer_handler(const uint64_t sched_time)=0;

public:
size_t _priority;
explicit C_timer(C_priority_task_queue& prio_queue, size_t priority);
virtual ~C_timer();

void set_timer(uint64_t sched_time);
int cancel();
};

base_timer.cpp

C_timer::C_timer(C_priority_task_queue& prio_queue, size_t priority):
_timer(prio_queue.io()), _timer_id(0), _prio_queue(prio_queue), _priority(priority){}

C_timer::~C_timer(){}

void C_timer::set_timer(uint64_t sched_time){
++_timer_id;

_timer.expires_at(std::chrono::time_point<std::chrono::high_resolution_clock>(std::chrono::milliseconds(sched_time)));
_timer.async_wait(_prio_queue.wrap(_priority, std::bind(&C_timer::timer_handler_internal, this,
std::placeholders::_1/*error*/, _timer_id, sched_time)));
}

int C_timer::cancel(){
++_timer_id;
return _timer.cancel();
}

void C_timer::timer_handler_internal(const asio::error_code& e, uint8_t timer_id,
const uint64_t sched_time){
if(e==asio::error::operation_aborted || timer_id != _timer_id){
return;
}
timer_handler(sched_time);
}

测试类

class C_timer_test: public C_timer{
private:
int _period;

virtual void timer_handler(const uint64_t sched_time) override{
std::cout<<"timer fired"<<std::endl;

uint64_t current_time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
set_timer(current_time + _period);
}

public:
C_timer_test(C_priority_task_queue& prio_queue, int priority, int period):C_timer(prio_queue, priority), _periodo(period){}
virtual ~C_timer_test(){}
void run(uint64_t delay=0){
uint64_t time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
set_timer(time + delay);
}
};

问题是如果我执行这个:

int main()
{
asio::io_service io;
C_priority_task_queue prio_queue(io);
asio::io_service::work w(io);

C_timer_test ti1(prio_queue, 0, 2000);
ti1.run();
prio_queue.run();

return 0;
}

我遇到了段错误。

但是,如果我执行以下代码,它工作正常:

int main()
{
asio::io_service io;
C_priority_task_queue prio_queue(io);
asio::high_resolution_timer _timer1(io);
asio::io_service::work w(io);

C_timer_test ti1(prio_queue, 0, 2000);
ti1.run();
prio_queue.run();

return 0;
}

这两段代码之间的唯一区别是在第二个主要代码中,我添加了以下行 asio::high_resolution_timer _timer1(io);我没有在任何地方使用过。

调试程序我发现这一行的信号正在上升: func_(&owner, this, ec, bytes_transferred);在文件中 task_io_service_operation.hpp

我使用的是 asio 版本 1.10.6。

关于可能发生的事情有什么建议吗?

来自 gdb 的回溯:

gdb ./main 
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
This GDB was configured as "i686-linux-gnu".
(gdb) r
[libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x0805f0d4 in ?? ()
(gdb) backtrace
#0 0x0805f0d4 in ?? ()
#1 0x080529fb in asio::detail::task_io_service::do_run_one (this=0x805f030, lock=..., this_thread=..., ec=...) at /src/cpp/external_lib/asio/include/asio/detail/impl/task_io_service.ipp:371
#2 0x080526ce in asio::detail::task_io_service::run_one (this=0x805f030, ec=...) at /src/cpp/external_lib/asio/include/asio/detail/impl/task_io_service.ipp:169
#3 0x08052c68 in asio::io_service::run_one (this=0xbffff08c) at /src/cpp/external_lib/asio/include/asio/impl/io_service.ipp:71
#4 0x08051f32 in C_priority_task_queue::run (this=0xbffff094) at priority_task_queue.cpp:19
#5 0x08049ac3 in main () at main.cpp:46

这里有 MakeFile:

TARGET=main
SOURCES= main.cpp base_timer.cpp priority_task_queue.cpp
SOURCE_DIR=.
INCLUDE_LIB= -L/src/cpp/libcore
INCLUDE_DIR=-I/src/cpp/external_lib/asio/include \
-I/src/cpp/libcore/include
INSTALL_DIR=.
LIB=-pthread
CXX=g++
CFLAGS=-Wall -fexceptions -fpermissive -std=c++11 -DASIO_STANDALONE
CFLAGS_DEBUG = -g3 -DDEBUG
OBJDIR_DEBUG=obj
BINDIR_DEBUG=.
OBJECTS_DEBUG:= $(addprefix $(OBJDIR_DEBUG)/,$(SOURCES:.cpp=.o))

all: debug

$(OBJDIR_DEBUG)/%.o: $(SOURCE_DIR)/%.cpp
@test -d $(OBJDIR_DEBUG) || mkdir -p $(OBJDIR_DEBUG)
$(CXX) $(CFLAGS) $(CFLAGS_DEBUG) $(INCLUDE_DIR) -c $< -o $@

debug: $(OBJECTS_DEBUG)
@test -d $(BINDIR_DEBUG) || mkdir -p $(BINDIR_DEBUG)
$(CXX) -o $(BINDIR_DEBUG)/$(TARGET) $^ $(INCLUDE_LIB) $(LIB)

更新

根据我的调查,我发现如果我在 .h 中定义 base_timer 成员(基本上是 asio::high_resolution_timer)初始化,代码运行正常,但如果我在 .cpp 中这样做,代码崩溃。

我的意思是,

explicit C_timer(C_priority_task_queue& prio_queue, size_t priority):
_timer(prio_queue.io()), _timer_id(0), _prio_queue(prio_queue), _priority(priority){}

在 .h 中有效,但是

C_timer::C_timer(C_priority_task_queue& prio_queue, size_t priority): 
_timer(prio_queue.io()), _timer_id(0), _prio_queue(prio_queue), _priority(priority){}

.cpp 失败

最佳答案

除了与 chrono(时间点或持续时间,选择一个!)的“不自然”杂耍之外,代码看起来还不错。我无法重现任何故障(GCC、Linux):

Live On Coliru

如果添加随机变量使问题出现/消失,您应该考虑未定义行为,使用静态分析、valgrind/purify/...和代码审查来找到您的罪魁祸首。 Valgrind、ASAN 和 UBSAN 在我的 PC 上运行干净

@sehe thanks for your effort. The diference is If I put every code into a single file, main.cpp, it runs, but if I separate into several files, the problem still there. In other hand, If I instanciate a high_resolution_timer object in the main.cpp, no matter the place (inside main(), in separate function that never is called, ...) it runs, but without it, a segmentation fault is raised.

太好了:您找到了 UB 的潜在来源:查看非 ODR 安全的静态变量或内联函数的使用。 (仔细检查所有翻译单元是否使用相同的编译器标志)。

此外,请记住 UB UNDEFINED,所以就像添加一个不相关的 _timer1 改变明显的行为(不改变 UB 的 source)一样使其看起来有效。

它在我的机器上运行干净的事实告诉你这必须是特定于平台的 UB 源

关于c++ - async_wait 中的 asio high_resolution_timer 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36038343/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com