Libevent programming troubleshooting and libevent troubleshooting

Source: Internet
Author: User

Libevent programming troubleshooting and libevent troubleshooting

Reprinted please indicate the source: http://blog.csdn.net/luotuo44/article/details/39547391



I read the source code of libevent some time ago. After reading this article, some questions about using libevent have become clear. For the libevent source code analysis, go to http://blog.csdn.net/luotuo44/article/category/2435521.pdf.

In this article, I hope to help others answer some questions when using libevent.


Can a single file descriptor be associated with multiple events?

The same file descriptor (fd) can call event_new multiple times to generate different events. These events with the same fd can have different callback functions and callback parameters. The events they listen to can also be different (this may be the reason for associating multiple events ).

If multiple events listen to the same event of the same fd, such as a readable event. When the fd becomes readable, all the callback functions that listen to the event will be called (that is, the event is triggered ). The callback function of the event that does not listen to this event is not called. If two events listen to different events of the same fd, their triggers are independent of each other.

 

 

Can a time-out event call the event_add Function Multiple times?

A time-out event can call the event_add function multiple times. In fact, all events can call the event_add function multiple times. However, only the time-out event can be called multiple times. Other events will be found multiple times and then returned ).

If the timeout value is different for each call to event_add, the last call prevails. To cancel the timeout and change the event to a normal event, set the second parameter of event_add to NULL.

 

 

Does the implementation of timeout events depend on the system time?

You can also ask: if a time-out event is used, will it affect the user's manual modification of the system time?

If the system supports MONOTONIC time, it will not be affected. If not, there will be some impact, that is, the timeout is not so accurate. For MONOTONIC time, refer to here. You can use the following code to test whether your system supports MONOTONIC time.

Struct timespects; if (clock_gettime (CLOCK_MONOTONIC, & ts) = 0) printf ("\ n supported"); elseprintf ("\ n not supported ");

If it is not modified frequently, if it is modified only once, the time-out only after the first modification may (only possible) be less accurate, the later timeout is accurate (if the event has the EV_PERSIST option ). Why do we say "only" and "not so? Because it depends on the system time in which the Libevent is modified. For details, refer to http://blog.csdn.net/luotuo44/article/details/38661787109t2.

 



Can I capture a signal and use a signal event to capture it at the same time?

No !!

Because the principle of implementing a signal event in Libevent is to set a signal capture function (also called a unified event source) for the signal ). Readers familiar with signal capturing should understand that there is only one function for signal capturing. If you set another one, the previous one will be overwritten.

 

 

 

In Linux, does Libevent use epoll by default?

If your system supports epoll, epoll is preferred. In fact, Libevent always gives priority to high-performance multi-channel I/O multiplexing functions (in Windows, it is an exception and does not give priority to IOCP ).

 

 

How do I know which multiplexing function is used by libevent?

After you call event_base_new to obtain an event_base, you can determine which multiplexing function to use. Call the event_base_get_method function to obtain the multiplexing function used by event_base. This function returns a string containing the names of multiple IO multiplexing functions such as "select", "poll", and "epoll. However, on Windows, "win32" is returned ". Generally, select is selected on Windows. For how to use IOCP, refer to the next one.


 

In Windows, how does one use IOCP?

In Windows, Libevent has little support for IOCP. IOCP is supported only in the connection listener evconnlistener and bufferevent socket. In addition, because Libevent selects select by default on Windows, Libevent uses IOCP only by setting EVENT_BASE_FLAG_STARTUP_IOCP macro.

You can use the event_config_set_flag function to set this macro. Specific content can refer to settings, can refer to the http://blog.csdn.net/luotuo44/article/details/38443569#t5

 


When will the evthread_use_pthreads function be called?

To use multithreading, thread security is required. Therefore, you must call this function before calling event_base_new (the corresponding Windows version is evthread_use_windows_threads ). If evthread_use_pthreads is called only after event_base_new, The event_base will not be thread-safe. For the principle, see http://blog.csdn.net/luotuo44/article/details/38501341425t0.

Note: This function only ensures the Libevent thread security. To use multiple threads, you must write your own code. The code in Libevent does not use multithreading. It only uses locks and conditional variables.

 


 

Libevent allows customization of memory allocation, logs, and thread locks. Do these customization have order requirements?

Yes. First, the customization of these three items should be put in front of the program, ensure that they are placed before any other libevent API call. Second, the three are also ordered. They should be in the order of memory allocation, logging, and thread locks.

 

 

Can I add an event in the next thread call event_add?

Yes. To ensure security, make sure that your program calls the evthread_use_pthreads function at the beginning. If the main thread is not calling the callback function of another event, the event will be immediately added to the listening queue by the main thread, and wait for the event together with other events.

 


Which Libevent functions are thread-safe?

If you think a function should be thread-safe, the author of Libevent will also know that the function is thread-safe.

After calling the evthread_use_pthreads function, you can use the function provided by Libevent with confidence. It always locks when a lock is required to ensure thread security.

 

 

Is bufferevent thread secure?

You added the BEV_OPT_THREADSAFE option when calling bufferevent_socket_new, so the thread is secure.

 

 

Is bufferevent_write asynchronous?

It is asynchronous, or non-blocking. After the call, it is not blocked and can be returned immediately.



 

Since bufferevent_write returns immediately, can the user's data be deleted after the return?

If the reader has tried to write non-blocking socket sending code using only the System Network API provided by the OS, the code will surely be overwhelmed by non-blocking traffic. You have to consider that the data to be sent is not completely sent in a write call. At this time, you have to find a place to save the data to be sent and use it when you call write next time. However, finding a place is annoying.

Although bufferevent_write is asynchronous and non-blocking, it is nice. When it returns, it copies all the data to be sent and stores it in the internal buffer zone. Therefore, the data to be sent can be discarded after the bufferevent_write is returned. You do not need to worry about saving the data.

 

 

Is the read event of bufferevent triggered horizontally or by edge?

The read event of bufferevent is triggered by edge. That is to say, if the client sends 100 bytes of data to the server, and the client only sends data once. After the server triggers a read event, all the 100 bytes should be read (assuming these 100 bytes arrive together ). You should not think about it. This callback reads only 4 bytes (such as length information) and then reads other data after the next Callback. Because the client only sends data once, there will be no more next Callback, even if there is data in the buffer zone of bufferevent.

Of course, if the client sends data again, the readable callback function of bufferevent will be called again.

For details, refer to http://blog.csdn.net/luotuo44/article/details/39344743109t6.

 

 

What does bufferevent read event high level mean?

The low level of read events is easy to understand. When the data in the bufferevent read buffer reaches this low level, the readable callback function set by the user will be called. For example, if you set a low water level of 4 bytes, it is not worth processing because you think that less than 4 bytes. When the data volume in the read buffer of bufferevent is smaller than 4 bytes, the user's readable callback function is not called. When the data volume is greater than or equal to 4 bytes, the user's readable callback function is called.

What is the high level of reading events? By default (that is, no high water level is set), once the socket fd has data readable, then, the libevent reads data from the socket fd kernel buffer to the bufferevent read buffer. When the client sends a large amount of data to the server, the server constantly copies the data to the bufferevent buffer. In this case, the TCP Sliding Window Protocol is useless.

The high level of the read event emerged at this time. When the data volume in the bufferevent read buffer reaches this high level, data will no longer be read from the socket fd. In this case, the socket fd kernel buffer will pile up a large amount of data, and the Sliding Window Protocol will work. When the number of read buffers of bufferevent is less than the high level, libevent can read data from the buffer zone of socket fd. Libevent is responsible for all the operations to stop reading and restore reading.

 



Why cannot I send data when the thread calls bufferevent_write?

In this case, the main thread runs in event_base_dispatch. The user wants to call bufferevent_write in the next thread to send data.

First, make sure that you have called the evthread_use_pthreads function (evthread_use_windows_threads function for Windows ).

Second, make sure you call the function before event_base_new.

 






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.