简体   繁体   English

libevent`struct event`变量可以修改吗?

[英]can libevent `struct event` variable be modified?

in on callback function on_accept , I make a event conn_ev 在on回调函数on_accept ,我创建了一个事件conn_ev

    conn_ev = (struct event *)malloc(sizeof(struct event));
    event_set(conn_ev, connfd, EV_READ, on_recv, conn_ev);
    event_base_set(base, conn_ev);
    event_add(conn_ev, NULL);

the callback function on_recv will be triggered when there is a new connection. 建立新连接后,将触发回调函数on_recv

and in the callback function on_recv(int connfd, short event, void *conn_event) , I have 在回调函数on_recv(int connfd, short event, void *conn_event) ,我有

    conn_ev = (struct event *) conn_event;
    event_set(conn_ev, connfd, EV_WRITE, on_send, conn_ev);
    event_base_set(base, conn_ev);
    event_add(conn_ev, NULL);

so the conn_ev is modified in this callback function. 因此,在此回调函数中修改了conn_ev

are there any possible problem/trap here so that it is better I malloc a new conn_ev? 是否有任何可能出现的问题/陷阱这里,所以它更好,我malloc新conn_ev?

thanks! 谢谢!

You have to make sure that the event is not used before modifying it. 您必须确保在修改事件之前不使用该事件。 If you are in the callback of this particular event and it's not a persistent one, you may be fine, but I would advise to call event_del() in any case. 如果您处于此特定事件的回调中,并且它不是持久性事件,则可以,但在任何情况下都建议调用event_del()。

While it may work, you should not use malloc() and event_set (which is now deprecated). 尽管可能有效,但您不应使用malloc()和event_set(现已弃用)。 Use event_new() instead, or at least replace event_set with event_assign(). 请改用event_new(),或至少将event_set替换为event_assign()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM