Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <pthread.h>
  5.  
  6. void* function1(void* _signal) {
  7.     int signal = *(int*)_signal;
  8.     sigset_t set;
  9.     siginfo_t info;
  10.     sigfillset(&set);
  11.     sigdelset(&set, SIGRTMIN);
  12.     pthread_sigmask(SIG_SETMASK, &set, NULL);
  13.  
  14.     sigemptyset(&set);
  15.     sigaddset(&set, SIGRTMIN);
  16.  
  17.     while (1) {
  18.         sigwaitinfo(&set, &info);
  19.         printf("Received %u thread with value %d; Signal -> %d\n", (int)pthread_self(), info.si_value.sival_int, info.si_signo);
  20.     }
  21.  
  22.     return NULL;
  23. }
  24.  
  25. void* function2(void* _signal) {
  26.     int signal = *(int*)_signal;
  27.     sigset_t set;
  28.     siginfo_t info;
  29.     sigfillset(&set);
  30.     sigdelset(&set, SIGRTMIN+1);
  31.     pthread_sigmask(SIG_SETMASK, &set, NULL);
  32.  
  33.     sigemptyset(&set);
  34.     sigaddset(&set, SIGRTMIN+1);
  35.  
  36.     while (1) {
  37.         sigwaitinfo(&set, &info);
  38.         printf("Received %u thread with value %d; Signal -> %d\n", (int)pthread_self(), info.si_value.sival_int, info.si_signo);
  39.     }
  40.  
  41.     return NULL;
  42. }
  43.  
  44. int main() {
  45.     pthread_t tid1, tid2;
  46.     sigset_t set;
  47.  
  48.     // Blokujemy sygnaly 41 oraz 42
  49.     sigemptyset(&set);
  50.     sigaddset(&set, SIGRTMIN);
  51.     sigaddset(&set, SIGRTMIN+1);
  52.     pthread_sigmask(SIG_BLOCK, &set, NULL);
  53.  
  54.     pthread_create(&tid1, NULL, function1, NULL);
  55.     pthread_create(&tid2, NULL, function2, NULL);
  56.     pthread_join(tid1, NULL);
  57.     pthread_join(tid2, NULL);
  58.  
  59.     return EXIT_SUCCESS;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement