I want to suspend the thread and resume it. There are few methods listed here. But I thought of using pause()
library function from unistd.h
.
What are the pitfalls of using pausing in signal handler?
One I noticed is, when I send 0
to pause thread and send 0
again then my signal is queued. I need to send 1
twice to resume the thread.
I guess there may be many more cases like this. How to handle such conditions if I want to use pause()
or sleep()
in signal handler.
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
static bool thread_ready = false;
static void cb_sig(int signal)
{
if (signal == SIGUSR1)
pause();
else if (signal == SIGUSR2)
;
}
static void *thread_job(void *ignore)
{
int i = 0;
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = cb_sig;
if (sigaction(SIGUSR1, &act, NULL) == -1)
printf("unable to handle siguser1\n");
if (sigaction(SIGUSR2, &act, NULL) == -1)
printf("unable to handle siguser2\n");
thread_ready = true;
while (1) {
printf("thread counter: %d\n", i++);
sleep(1);
}
return NULL;
}
int main()
{
bool running;
int user_input;
pthread_t thread;
if (pthread_create(&thread, NULL, thread_job, NULL))
return -1;
while (!thread_ready);
for (running = true; running; ) {
printf("0: pause thread, 1: resume thread, -1: exit\n");
scanf("%d", &user_input);
switch(user_input) {
case -1:
running = false;
break;
case 0:
pthread_kill(thread, SIGUSR1);
break;
case 1:
pthread_kill(thread, SIGUSR2);
break;
}
}
pthread_kill(thread, SIGKILL);
return 0;
}