Advertisement
Guest User

Singals and pthreads min. example

a guest
Mar 30th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include<pthread.h>
  2. #include<stdio.h>
  3. #include<signal.h>
  4. #include<unistd.h>
  5.  
  6. void sig_handler(int signo)
  7. {
  8.     printf("received SIGUSR1\n");
  9.     _exit(0);
  10. }
  11.  
  12. void *theOtherThread(void*) {
  13.     while(true) {} // ∞ cycle
  14. }
  15.  
  16. int main() {
  17.     if (signal(SIGUSR1, sig_handler) == SIG_ERR)
  18.         printf("\ncan't catch SIGUSR1\n");
  19.     pthread_t thr;
  20.     if (pthread_create(&thr, 0, theOtherThread, 0)) { // invoke a child thread
  21.         perror("Creating thread");
  22.         return 1;
  23.     }
  24.     sleep(1); // sleep one second
  25.     pthread_kill(thr, SIGUSR1); // send signal to the child thread
  26.     if (pthread_join(thr, 0)) { // wait for the child thread to exit
  27.         perror("Creating thread");
  28.         return 1;
  29.     }
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement