Skip to content

Instantly share code, notes, and snippets.

@azat
Created April 22, 2018 23:32
Show Gist options
  • Save azat/a251a7b792f6cbd9d45583ce2495402a to your computer and use it in GitHub Desktop.
Save azat/a251a7b792f6cbd9d45583ce2495402a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <event2/event.h>
#include <event2/thread.h>
void
cb( evutil_socket_t fd, short what, void *arg )
{
return;
}
void *
worker( void *arg )
{
struct event_base *base = arg;
printf( "Worker: Starting up\n" );
if ( event_base_dispatch( base ) == 1 ) {
printf( "Worker: Successfully exited when running out of events\n" );
}
}
int
main( int argc, char **argv )
{
struct event_base *base;
struct event *event;
pthread_t tid;
evthread_use_pthreads();
base = event_base_new();
event = event_new( base, -1, EV_READ, cb, NULL );
event_add( event, NULL );
printf( "Main thread: starting worker\n" );
pthread_create( &tid, NULL, worker, base );
sleep(1);
printf( "Main thread: activating last pending event\n" );
event_active( event, 0, 1 );
sleep(1);
printf( "Main thread: removing last pending event\n" );
event_del( event );
#if (EVENT__NUMERIC_VERSION >= 0x02010000)
if ( argc > 1 ) {
printf( "Main thread: signalling base\n" );
event_base_loopcontinue( base );
}
#endif
pthread_join( tid, NULL );
printf( "Main thread: worker finished, cleaning up\n" );
event_free( event );
event_base_free( base );
#if (EVENT__NUMERIC_VERSION >= 0x02010000)
libevent_global_shutdown();
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment