Reprint Please specify Source: http://blog.csdn.net/luotuo44/article/details/39325447
Lock Operation:
In the previous blog post you can see that many functions need to lock the evbuffer before operation. Unlike Event_base, if the evbuffer supports locks, the function evbuffer_enable_locking is called explicitly.
[CPP] View Plain copy//buffer.c file int//parameter can be a lock variable or null evbuffer_enable_locking (struct evbuffer *buf, void *lock) { #ifdef _event_disable_thread_ support return -1; #else if (buf->lock) return -1; if (!lock) { //self-assigning lock variables evthread_alloc_lock ( lock, evthread_locktype_recursive); if (! Lock) return -1; buf->lock = lock; //the Evbuffer has a lock, it needs to release the lock memory buf->own_lock = 1; } else { buf->lock = lock;//lock with parameters buf->own_lock = 0;//himself did not own the lock. No need to release lock memory } return 0; #endif }
As you can see, the second argument can be null. A lock is requested inside the function. Obviously if you want Evbuffer to be able to use a lock, you have to call Evthread_use_windows_threads () or evthread_use_pthreads () at the beginning, and you can refer to a blog post.
Because the user controls this Evbuffer, Libevent provides a lock and unlock interface for the user to use.[CPP] View Plain copy//buffer.c file void evbuffer_lock (struct evbuffer *buf) { evbuffer_lock (buf); } void Evbuffer_unlock (struct evbuffer *buf) { evbuffer _unlock (BUF); } //evbuffer-internal.h file #define evbuffer_ LOCK (buffer) \ do { \ evlock_lock ((buffer)->lock, 0); \