How can I wake up paused thread using only signal() or pthread_kill()?
I think paused thread must recieved some kind of signals to activate, but I don't know what signal have to be sent to paused thread.
//My Terminal
USER32@myLaptop:~/list$ gcc -o ./temp ./temp.c -lpthread
USER32@myLaptop:~/list$ ./temp
Starting Thread...
//My Codes
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
pthread_t tid;
void *thread_function(void *data){
tid = pthread_self();
write(STDOUT_FILENO, "Starting Thread...\n", sizeof("Starting Thread...\n"));
pause();
write(STDOUT_FILENO, "Success!\n", sizeof("Success!\n"));
}
int main(){
pthread_t pth;
pthread_create(&pth, NULL, &thread_function, NULL);
sleep(1);
pthread_kill(tid, SIGCONT);
pthread_join(pth, NULL);
return 0;
}
The pause()
function will return on any signal that can be caught, but you have to set up a signal handler first and catch that signal.
Here is an example:
void signal_handler(int sig) {
printf("Caught signal %d\n", sig);
}
void *thread_function(void *data) {
signal(SIGUSR1, signal_handler);
//rest of your code here
}
int main() {
...
sleep(1);
pthread_kill(tid, SIGUSR1);
...
}
SIGUSR1
is one of the signals reserved for internal use by applications so it is suitable for this case.
SIGCONT
can be caught, but it is not designed for this, but instead to resume processes that where stopped by SIGSTOP
.