Libevent
Source code in-depth analysis 13
--
Libevent
Signal Processing notes
As mentioned above
Libevent
Multiple Threads are implemented. However, when registering a signal event in a multi-threaded environment, you must be careful when dealing with the event.
Libevent
Register a signal event on the instance. Append the title
Libevent
Series.
To
2
For example, to do a simple scenario analysis.
1
First, create and initialize a thread.
1
Of
Libevent
Instance
Base1
, Thread
1
Of
Libevent
Instance
Base2
;
2
In
Base1
Register on
Sigalrm
Signal; in
Base2
Register on
SIGINT
Signal;
3
Assume that the current
Base1
And
Base2
No other events are registered;
4
Thread
1
And
2
All
Event_base_loop
Event loop:
Event_base_loop (base1)
Event_base_loop (base2)
{
{
If (base2-> Sig. ev_signal_added)
If (base2-> Sig. ev_signal_added)
Evsignal_base = base1;
Evsignal_base = base2;
While (! Done)
While (! Done)
{
{
...
...
Evsel-> dispatch (...);
Evsel-> dispatch (...);
...
...
}
}
}
}
5
Suppose thread
1
Enter
Event_base_loop
And set
Evsignal_base = base1
; And wait;
6
Next thread
2
Also enter
Event_base_loop
And set
Evsignal_base = base2
; And wait;
Therefore
Evsignal_base
Point
Base2
;
7
Signal
Alarm
Trigger, call the service routine:
Static void evsignal_handler (INT sig)
{
...
Evsignal_base-> Sig. evsigcaught [sig] ++;
Evsignal_base-> Sig. evsignal_caught
= 1;
/*
Wake up our notification mechanic */
Send (evsignal_base-> Sig. ev_signal_pair [0],
"A", 1, 0 );
...
}
Therefore
Base2
Notified
Alarm
The signal happened, but actually
Alarm
Yes registered in
Base1
On,
Base2
On
Alarm
Register
Event
It is null, so the handler cannot be called;
Therefore
Libevent
To process the signal, you can only register the signal to one
Libevent
Instance.
Memcached
No
Libevent
Provided
Signal
Interface, but directly use the native
API
, It looks more concise.