1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| #include <cstdlib>
#include <cstdio>
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
using namespace std;
#define RESUME_SIG SIGUSR2
#define SUSPEND_SIG SIGUSR1
sigset_t mask, maskold, maskwait,signal_mask;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static void *thread(void *arg)
{
//Masque tous les signaux à part ce qui suspende et lance la thread
sigset_t maskthread;
sigfillset(&maskthread);
sigdelset(&maskthread, RESUME_SIG);
sigdelset(&maskthread, SUSPEND_SIG);
pthread_sigmask(SIG_SETMASK, &maskthread, NULL);
while(1)
{
usleep(10000);
std::cout << "Thread " << std::endl;
}
pthread_exit(NULL);
}
void suspend_thread(int signal)
{
std::cout << "SIGSUSPEND" << std::endl;
}
void resume_thread(int signal)
{
cout << "SIGRESUME" << endl;
}
int main()
{
struct sigaction sa,su;
pthread_t th;
int id = 1, iteration = 0;
bool w1 = false;
sigemptyset(&mask);
sigaddset(&mask, RESUME_SIG);
sigaddset(&mask, SUSPEND_SIG);
sigprocmask(SIG_BLOCK,&mask,NULL);
pthread_sigmask(SIG_BLOCK, &mask, NULL);
sigfillset(&sa.sa_mask);
sigdelset(&sa.sa_mask, SUSPEND_SIG);
sa.sa_flags = 0;
sa.sa_handler = suspend_thread;
sigaction(SUSPEND_SIG, &sa, NULL);
sigfillset(&su.sa_mask);
sigdelset(&su.sa_mask, RESUME_SIG);
su.sa_flags = 0;
su.sa_handler = resume_thread;
sigaction(RESUME_SIG, &su, NULL);
pthread_create(&th, NULL, thread, NULL);
while(1)
{
usleep(100000);
iteration++;
std::cout << "Main " << iteration << std::endl;
if(w1 == false)
{
std::cout << "Sending Pause"<< std::endl;
w1 = true;
pthread_kill(th, SUSPEND_SIG);
std::cout << "End Sending Pause "<< std::endl;
}
else
{
std::cout << "Sending Resume"<< std::endl;
w1 = false;
pthread_kill(th, RESUME_SIG);
std::cout << "End Sending Resume"<< std::endl;
}
}
return 0;
} |
Partager