- From:
Http://blog.csdn.net/xian0617/article/details/6689357
- /*
- * Waitquitsignal. h
- *
- * Created on: Aug 14,201 1
- * Author: xian0617
- */
- # Ifndef waitquitsignal_h _
- # Define waitquitsignal_h _
- # Include <signal. h>
- # Include <time. h>
- Class waitquitsignal {
- Public:
- Static void Init ();
- Static bool wait (bool & flag );
- PRIVATE:
- Static sigset_t m_wait_mask;
- Static struct timespec m_time;
- };
- # Endif/* waitquitsignal_h _*/
/* <Br/> * waitquitsignal. h <br/> * created on: Aug 14,201 1 <br/> * Author: xian0617 <br/> */</P> <p> # ifndef waitquitsignal_h _ <br/> # define waitquitsignal_h _ <br/> # include <signal. h> <br/> # include <time. h> <br/> class waitquitsignal {<br/> Public: <br/> static void Init (); <br/> static bool wait (bool & flag ); <br/> PRIVATE: <br/> static sigset_t m_wait_mask; <br/> static struct timespec m_time; <br/> }; </P> <p> # endif/* waitquitsignal_h _ */<br/>
[CPP]
View plaincopyprint?
- /*
- * Waitquitsignal. cpp
- *
- * A thread in Linux is essentially a light weighted process. When a thread is generated, a corresponding process control structure is generated,
- * This structure shares the same process memory space with the parent thread's process control structure. At the same time, the process control structure of the new thread will be from the parent thread (process)
- * Obtain the same process information, such as opening the file list and blocking mask. Because we modified the signal blocking mask after the subthread was generated
- * Code. At this moment, the subthread uses the original process information of the main thread. Therefore, the subthread will still respond to the SIGINT and sigterm signals. Therefore, when
- * When we use Ctrl + C to send a SIGINT signal, the main process does not process the signal, and the sub-process (thread) performs default processing, that is, exit.
- * When a child process exits, it sends a sigchld signal to the parent process (thread), indicating that the child process exits. Because the signal is not blocked
- * The main process (thread) also exits immediately, and the preceding running condition occurs. Therefore, one solution to this problem is to set the signal before the subthread is generated,
- * Set the signal within the subthread.
- * Created on: Aug 14,201 1
- * Author: xian0617
- */
- # Include <iostream>
- # Include "waitquitsignal. H"
- Sigset_t waitquitsignal: m_wait_mask;
- Struct timespec waitquitsignal: m_time;
- // Call this before thread create
- Void waitquitsignal: Init (){
- Try {
- Signal (sigkill, sig_ign );
- Sigemptyset (& m_wait_mask );
- Sigaddset (& m_wait_mask, SIGINT );
- Sigaddset (& m_wait_mask, sigquit );
- Sigaddset (& m_wait_mask, sigterm );
- Pthread_sigmask (sig_block, & m_wait_mask, 0 );
- } Catch (STD: exception & E ){
- STD: cerr <"exception:" <E. What () <STD: Endl;
- }
- M_time. TV _sec = 0;
- M_time. TV _nsec = 0;
- }
- Bool waitquitsignal: Wait (bool & flag ){
- Try {
- Siginfo_t SIG;
- Switch (sigtimedwait (& m_wait_mask, & Sig, & m_time )){
- Case SIGINT:
- Case sigquit:
- Case sigterm:
- Flag = false;
- Break;
- Default:
- Break;
- }
- } Catch (STD: exception & E ){
- STD: cerr <"exception:" <E. What () <STD: Endl;
- }
- Return flag;
- }
/* <Br/> * waitquitsignal. CPP <br/> * in Linux, a thread is essentially a light weighted process. When a thread is generated, a corresponding process control structure is generated, <br/> * This structure shares the same process memory space with the parent thread's process control structure. At the same time, the process control structure of the new thread will copy the same process information from the parent thread (process) <br/> *, such as opening the file list and blocking mask. Because the signal blocking mask is modified after the subthread is generated <br/> * code, the subthread uses the original process information of the main thread, therefore, the sub-thread will still respond to the SIGINT and sigterm signals. Therefore, when <br/> * we use Ctrl + C to send a SIGINT signal, the main process will not process the signal, the sub-process (thread) performs default processing, that is, exit. <Br/> * when a child process exits, it sends a sigchld signal to the parent process (thread), indicating that the child process exits. Because the signal is not blocked, as a result, <br/> * The main process (thread) also exits immediately and the preceding running condition occurs. Therefore, one solution to this problem is to set the signal before the subthread is generated. <br/> * or set the signal inside the subthread. <Br/> * created on: Aug 14,201 1 <br/> * Author: xian0617 <br/> */</P> <p> # include <iostream> <br/> # include "waitquitsignal. H "<br/> sigset_t waitquitsignal: m_wait_mask; <br/> struct timespec waitquitsignal: m_time; <br/> // call this before thread create <br/> void waitquitsignal: Init () {<br/> try {<br/> signal (sigkill, sig_ign ); <br/> sigemptyset (& m_wait_mask); <br/> sigaddset (& m_wait_mask, SIGINT); <br/> sigaddset (& m_wait_mask, sigquit ); <br/> sigaddset (& m_wait_mask, sigterm); <br/> pthread_sigmask (sig_block, & m_wait_mask, 0); <br/>} catch (STD: exception & E) {<br/> STD: cerr <"exception:" <E. what () <STD: Endl; <br/>}< br/> m_time. TV _sec = 0; <br/> m_time. TV _nsec = 0; <br/>}< br/> bool waitquitsignal: Wait (bool & flag) {<br/> try {<br/> siginfo_t SIG; <br/> switch (sigtimedwait (& m_wait_mask, & Sig, & m_time) {<br/> case SIGINT: <br/> case sigquit: <br/> case sigterm: <br/> flag = false; <br/> break; <br/> default: <br/> break; <br/>}</P> <p>} catch (STD: exception & E) {<br/> STD: cerr <"exception: "<E. what () <STD: Endl; <br/>}< br/> return flag; <br/>}< br/>
Sample Model
[CPP]
View plaincopyprint?
- Int main (INT argc, char ** argv ){
- // Step 1 init
- Waitquitsignal: Init ();
- // Step 2 create subthread
- // Step 3 Wait Signal
- While (waitquitsignal: Wait (FLAG )){
- Sleep (1 );
- }
- // Step 4 deal quit job
- // Save ();
- }
Int main (INT argc, char ** argv) {<br/> // Step1 init <br/> waitquitsignal: Init (); <br/> // step2 create subthread </P> <p> // Step3 wait signal <br/> while (waitquitsignal: Wait (FLAG )) {<br/> sleep (1); <br/>}< br/> // step4 deal quit job <br/> // save (); <br/>}