PovilasB
PovilasB

Reputation: 1608

Libevent. Configuring event_base with thread support

According to libevent documentation there's a function to make event_base structure thread safe:

EVENT2_EXPORT_SYMBOL int evthread_make_base_notifiabe(struct event_base* base)

Documentation also states that: "You shouldn't need to call this by hand; configuring the base with thread support should be necessary and sufficient."

There's:

EVENT2_EXPORT_SYMBOL struct event_base* event_base_new_with_config(const struct event_config *) 
EVENT2_EXPORT_SYMBOL int event_config_set_flag (struct event_config *cfg, int flag) 

But I can't just figure out how to configure event_base to get the same effect as with evthread_make_base_notifiable. Any ideas?

Upvotes: 1

Views: 3693

Answers (2)

Jose Quinteiro
Jose Quinteiro

Reputation: 479

According to the Libevent book, you have to tell Libevent which locking functions to use. You do this by invoking either evthread_use_windows_threads() or evthread_use_pthreads() depending on your platform. You need to do this before you call any Libevent function that allocates a structure that needs to be shared between threads. You'll also need to add something like -levent_thread to your linker flags.

Upvotes: 2

walla
walla

Reputation: 303

If you create the event base like this:

struct event_config* config = event_config_new();

if ((m_base = event_base_new_with_config(config)) == NULL)
{
    cout << "Failed to create an event base";
}

event_config_free(config);

Then the event base will be thread safe (at least the event_add and event_base_once functions which are enough to remove the thread-safety issue by creating an event with the task and run it on the right thread).

In http://www.wangafu.net/~nickm/libevent-book/Ref2_eventbase.html is written that you can configure the no lock flag (EVENT_BASE_FLAG_NOLOCK) so by default it is locked. All this may not be relevant to very old versions of libevent where locking mechanism hasn't yet introduced.

Upvotes: 0

Related Questions