nmathewson/Libevent — Issue #34: event re-ordering
Author: ghazel
Created: 2011-12-03 23:31:19+00:00
State: open

When queueing events from another thread, it seems the events do not always execute in the same order they were queued. Demo code which should fail within the first 10-20 events:

```c

#include
#include
#include

#include

#include
#include
#include
#include

struct timeval now = {0,0};

struct event_base *g_base = NULL;

struct notify_info {
int seq;
};

void wakeup_event(evutil_socket_t fd, short events, void *ctx)
{
struct notify_info *n = (struct notify_info*)ctx;
static int seq = 0;
printf("wakeup_event seq:%d n->seq:%d\n", seq, n->seq);
assert(n->seq == seq);
seq++;
}

void queue_event()
{
static int seq = 0;
struct notify_info *n = malloc(sizeof(struct notify_info));
n->seq = seq;
printf("virtual_fd_write seq:%d\n", seq);
event_base_once(g_base, -1, EV_TIMEOUT, wakeup_event, n, &now);
seq++;
}

void *queue_event_thread(void *userdata)
{
for (;;) {
queue_event();
}
}

int main()
{
#ifdef EVTHREAD_USE_PTHREADS_IMPLEMENTED
evthread_use_pthreads();
#elif defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED)
evthread_use_windows_threads();
#endif

g_base = event_base_new();
if (!g_base) {
printf("Couldn't open event base\n");
return 1;
}
if (evthread_make_base_notifiable(g_base) != 0) {
printf("Couldn't make base notifiable\n");
return 1;
}

pthread_t thread;
int rc = pthread_create(&thread, NULL, queue_event_thread, NULL);
if (rc) {
printf("ERROR: return code from pthread_create() is %d\n", rc);
return 1;
}

// keep it from exiting
void event_base_add_virtual(struct event_base *);
event_base_add_virtual(g_base);

printf("running\n");
event_base_dispatch(g_base);

return 0;
}

```