![](/img/trans.png)
[英]libevent: is it allowed to free an event inside its callback function if the event is created by malloc
[英]How to pass arguments to an event function created using libevent?
我目前正在嘗試使用libevent每n分鍾調用一次函數/一個將觸發該函數的事件觸發器,並建議可以使用libevent。 我可以使用它每n秒持久調用一次函數,但是無法確定如何將參數傳遞給它們。
#include <stdio.h>
#include <sys/time.h>
#include <event.h>
void say_hello(int fd, short event, void *arg)
{
printf("Hello\n");
// printf("%d", (int *)arg[0])
}
int main(int argc, const char* argv[])
{
struct event ev;
struct timeval tv;
tv.tv_sec = 3;
tv.tv_usec = 0;
event_init();
// event_set(&my_event, 0, EV_PERSIST, my_function, NULL);
event_set(&ev, 0, EV_PERSIST, say_hello, NULL);
// evtimer_set(&ev, say_hello, NULL);
evtimer_add(&ev, &tv);
event_dispatch();
return 0;
}
event_set
已被棄用,取而代之的是,使用event_new
:
struct event *event_new(
struct event_base *,
evutil_socket_t,
short,
event_callback_fn,
void *
)
最后一個參數是要傳遞的參數 :
Parameters
base the event base to which the event should be attached.
fd the file descriptor or signal to be monitored, or -1.
events desired events to monitor: bitfield of EV_READ, EV_WRITE, EV_SIGNAL, EV_PERSIST, EV_ET.
callback callback function to be invoked when the event occurs
callback_arg an argument to be passed to the callback function
要安排在event_new()
指定的事件的執行,請使用event_add()
,他的原型是:
int event_add(
struct event *ev,
const struct timeval *timeout
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.