On Sun, 2007-08-05 at 15:31 -0300, Victor wrote: > Is it safe to call event_add() from a second thread to > add more events on that event loop base?
No. Two threads cannot modify the same event_base struct without explicit locking. You would need to use a mutex to protect the event_add() call. On a related note... I would like to see a threaded version of libevent that would give each thread it's own current_base variable. Right now, there is a global variable in event.c: struct event_base *current_base = NULL; A threaded library would use thread-specific data instead: static pthread_key_t *current_base_key; #define current_base \ ((struct event_base *) (pthread_get_specific(current_base_key)) This would allow you to use event_add(), event_dispatch(), and event_loop() in multiple threads in a natural way. Each thread would get it's own event loop automatically and not interfere with other threads' operation. You could also add internal protection within the event_base_() functions with mutexes so that two threads would not be able to modify the same event_base struct simultaneously. My suggestion is to name this threaded version of the library `libevent_r.a` and have a separate Makefile target to generate the threaded version. This second target would add -D_REENTRANT to the CFLAGS to enable all of the threaded codepaths. You would also need to link it with the pthreads library by adding -lpthread to the LDFLAGS. Returning to my previous example, you would modify event.c to look something like this: #if _REENTRANT static pthread_key_t *current_base_key; # define current_base \ ((struct event_base *) (pthread_get_specific(current_base_key)) #else struct event_base *current_base = NULL; #endif This approach allows both the threadsafe and non-threadsafe library to be built from the same source code. Niels, does this sound like something you would be willing to consider? If so, I would be willing to do the work and submit a patch. Regards, Mark _______________________________________________ Libevent-users mailing list Libevent-users@monkey.org http://monkey.org/mailman/listinfo/libevent-users