, , . , - .
, . libevent pthreads, , , . :
#include <stdint.h>
#include <pthread.h>
#include <event.h>
void * thread_func (void *);
int main(void)
{
int32_t tid = 0, ret = -1;
struct event_base *evbase;
struct event *ev;
int32_t *t_ret = &ret;
struct timeval tv;
evthread_use_pthreads();
ret = pthread_create(&tid, NULL, thread_func, NULL);
evbase = event_base_new();
timer = event_new(evbase, -1, EV_PERSIST, callback_func, NULL);
tv.tv_sec = 0;
tv.tv_usec = 1000;
evtimer_add(timer, &tv);
event_base_dispatch(evbase);
event_free(timer);
event_base_free(evbase);
return 0;
}
void * thread_func(void *arg)
{
struct event *ev;
struct event_base *base;
base = event_base_new();
ev = event_new(base, -1, EV_PERSIST, thread_callback, NULL);
event_add(ev, NULL);
event_base_dispatch(base);
event_free(ev);
event_base_free(base);
pthread_exit(0);
}
, . :
- evthread_use_pthreads() libevent pthreads Linux ( ). evthread_use_window_threads(). , event.h.
- event_base , . .
- , , event. , -1 . , , , EV_PERSIST. .
- Schedule an event to run
- Start event loop
- free resources when done.
The version of Libevent used in my case, libevent2 5.1.9 , you will also need the libevent_pthreads.so library for linking.
greetings.
source
share